Android Application Components Tutorial
What will I learn?
- What are Android Application Components?
- What are the Different Types of Android Application Components?
- Activities
- Services
- Content Providers
- Broadcast Receivers
- How to Activate the above Components?
- Additional Components- Fragments, Resources, Layouts, Views, Intents, Manifest File.
Requirements
Basic Java programming
Difficulty
Basic
Introduction
What are Android Application Components?
Android application components are the building blocks of an Android application. Each component has its own point through which system can enter your application.
Each application component has its specific role. Each one is a unique building block that helps define your app's overall behavior.
Source
Types of Android Application Components
In the android mainly there are four different types of application components. Each component has it's specific purpose. They are :
Activities
Services
Content Providers
Broadcast Receivers
1. Activities:
Activity represents the presentation layer of an Android application, e.g. a screen which the user sees. An Android application can have several activities(screens) what's more, it can be exchanged between them amid runtime of the application. Basically a page in your application.
An action is actualized as a subclass of " Movement" and you can take in more about it in the Activities designer manage.
Example of Android Activity:
public class MainActivity extends Activity
{
// Called when the activity is first created.
@Override
protected void on\_Create(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\_main);
printMessage("onCreate");
}
}
Here the activity is MainActivity. It always extends Activity. The layout can be set in the setContentView(R.layout.activity_main);
2. Services:
Android Services are used for performing long-running operations in the background. If you perform long-running operations in the main thread or application thread that my causes to decrease the application speed. So in this case, we will create an android thread and the process will run here. This operation is called as a service in android.
A service does not give a UI. For example, a service may play music outside of anyone's ability to see while the customer is in a substitute application, or then again it might bring data over the framework without blocking customer coordinated effort with an activity.
Another portion, for instance, an activity, can start the service and let it run or attach to it remembering the true objective to coordinate with it.
Example for Android Service:
intent = new Intent(this, MyService.class);
startService(intent);
In the following example we will start the service by using intent:
public class MyService extends Service
{
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
showMessage("Service Started");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy()
{
super.onDestroy();
showMessage("Service Stopped");
}
private void showMessage(String msg)
{
Toast.makeText(this, msg, Toast.LENGTH\_SHORT).show();
}
}
3. Content Providers:
A Content provider manages the access to a central repository of data. You can store your data in the file system, sqlite database, on web or any other storage location. Though the content provider others apps can modify or query data in the application.
For example, the Android system provides a content provider that manages the user's contact information. As such, any app with the proper permissions can query part of the content provider (such as ContactsContract.Data) to read and write information about a particular person.
It is also useful reading and writing data that is private to your app and not shared.
Example of Android Content Providers:
uri = android.provider.MediaStore.Images.Media.EXTERNAL\_CONTENT\_URI;
Intent i = new Intent(Intent.ACTION\_PICK,uri);
startActivityForResult(i,RESULT\_LOAD\_IMAGE);
4. Broadcast Receivers:
Android BroadcastReceiver is a component where you can register for system or application component. You will be notified about the events after registering. Broadcast originates from the system as well as applications.
For example, if you want an alarm in the morning. so first you need to register your system by using broadcast. So that the alarm will come in the morning. You must add receiver tag in the manifest file.
Example of Android BroadcastReceiver:
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM\_SERVICE);
alarmManager.set(AlarmManager.RTC\_WAKEUP, calendar.getTimeInMillis() + (1000 \* 60), pendingIntent)
Any app can start another app's component. For example, if you want the user to capture a photo with the device camera, there's probably another app that does that and your app can use it, instead of developing an activity to capture a photo yourself.
You don't need to incorporate or even link to the code from the camera app. Instead, you can simply start the activity in the camera app that captures a photo. When complete, the photo is even returned to your app so you can use it. To the user, it seems as if the camera is actually a part of your app.
Activating the Android Application Components:
All these four components are activated by using Intent. Intent is used start one activity to another activity, service, provider or Broadcast. So by using intent we will activate those components.
In all the examples which we have provided above, we have used intent.
Additional Android Components:
There are additional components that are also used in the construction of above-mentioned entities, their logic, and wiring between them. These are:
- Fragments,
- Resources,
- Layouts,
- Views,
- Intents,
- Manifest File.
1. Fragments:
A fragment is a free part which can be utilized by an action. A piece epitomizes usefulness so it is less demanding to reuse inside exercises and designs. It keeps running with regards to an action, yet has its own lifecycle and regularly its own UI.
It is additionally conceivable to characterize pieces without a UI, i.e., headless sections. It can be progressively or statically added to a movement.
2. Resources:
If you want to externalize the images or strings in your application, That will maintain independently. In order to provide compatibility in your project, you must organize Res folder in your project.
3. Layouts:
An Android layout is a class that handles organizing the way its kids show up on the screen. Anything that is a View (or acquires from View) can be an offspring of a design.
The greater part of the designs acquire from ViewGroup (which acquires from View) so you can settle formats. You could likewise make your own custom design by making a class that acquires from ViewGroup. There are diverse kinds of designs in android.
Examples of Android Layouts:
- Linear layout
- Frame Layout
- Relative layout
- Absolutelayout
- Grid Layout
- Table Layout
4. Views:
Each layout is used to organize your widgets according to the requirement. View group is a collection of views. View Group is nothing but the organization of our views. ViewGroup is also called as layouts.
Examples of Android Views:
- Textview,
- Edittext,
- Button,
- Image Button,
- CheckBox,
- Toggle Button,
- Radiobutton,
5. Intents:
An Intent is basically a message to say you did or want something to happen. Depending on the intent, apps or the OS might be listening for it and will react accordingly.
6. Android Manifest File:
Applications declare their components in a manifest file that's bundled into the Android package, the .apk file that also holds the application's code, files, and resources. The manifest is a structured XML file and is always named AndroidManifest.xml for all applications.
It is also used for naming any libraries the application needs to be linked against (besides the default Android library) and identifying any permission the application expects to be granted.
Android Application Components Tutorial Conclusion:
Finally, By using these components android application will be designed. According to my experience, the intents and activity components are used in most of the cases. Remaining services, broadcast receivers, content providers are used depending on the usage.