In this article, we will try to make an overview of the Android Application Components. That is the various elements by which an Android Application is built.
(I assume you have read the article about Android architecture to get an overview of the platform architecture of Android OS)
There are mainly five types of components that are used to build an application. Actually, these are some classes defined in the Android SDK and provide different methods by which an application can behave. As a developer, we need only to call and extend these already defined classes to use in our application. These are the main Android Application Components:
- Content Providers
- Broadcast Receivers
You will learn how to use these Android application components to build applications later when we start actual development. Now I’m giving you brief descriptions of each term to make you familiar with the basic concepts.
Activity is an individual user interface screen in an Android Application where visual elements called Views (also known as widgets) can be placed and the user can perform various actions by interacting with it. Consider the figure.
The whole window gives the user an interface to interact with and therefore this complete screen makes an Activity. The controls placed in the window allows the user to perform certain actions and are called Views or Widgets. In this example, there are five widgets, they are TextView, EditText, AnalogClock and two Buttons.
The widgets in an Activity can be created in two different ways, by pure java code and by adding XML code to define the UI. The latter is always preferred. An application can have more than one Activity and each Activity operates independently but can be linked to one another and each Activity you create must be defined in your application’s manifest file. Each Activity in Android will be a subclass of Activity class defined in Android SDK.
A service is an Android application component that runs in the background and has no visual UI. Services are used to perform the processing parts of your application in the background. While the user is working on the foreground UI, services can be used to handle the processes that need to be done in the background. A service can be started by another Android application components such as an activity or other services and it will continue to run in the background even after the user switches to another application. Thus services are less likely to be destroyed by the Android system to free resources, than Activities.
One typical example of the use of services is a music player application. We can use an activity to select a music track from the SD card and to play it. When it starts playing, the user is able to open other applications and the music plays in the background. This can be made possible only by the use of Android Services. The Activity that chooses the music track actually invokes a service that works in the background. Then it will continue playing in the background even after the front end activity gets destroyed. At any time the user is able to come back to the activity and use the seek bar to seek the track, select another track, or end playback. This means that the service and the Activity that invoked the service are not completely independent, instead, the Activity is able to fully control the Service.
Another example of a service is the downloading of a file from the internet. It should run in the background and continue downloading even after we switch to other applications.
All Android services are implemented as a subclass of Service class defined in Android SDK. There are two types of services in Android.
Its a type of service which is not bounded to any components. Once started, it will run in the background even after the component that started the service gets killed. It can be run in the background indefinitely and should stop by itself after the operation is intended to carry out is completed.
Its bound to other components and runs only till the component to which it is bounded runs.
3. Content Providers
Content providers are one Android application components that provide a flexible way to make data available across applications. Suppose you are creating any type of data in your application (For example consider you are creating a to-do list in your application, then the list of things is a data) and you are storing it at any storage location, it may be in the database, file system or in any online storage space. Then through content providers, other applications are able to query, access or even modify the data you’ve created, as long as your content provider allows it. In a similar way, you can access the data that other utilities have created, by using content providers. Example for a content provider in Android is the contacts database. The Content provider of contacts database allows other applications to query, read, modify, and write the contacts info. Android comes with several other built-in Content providers that we can use in our application. All content providers are implemented as a subclass of ContentProvider class which is defined in Android SDK.
4. Broadcast Receivers
Broadcast receivers are one of the Android application components that is used to receive messages that are broadcasted by the Android system or other Android applications. There are many broadcasts that are initiated by the Android system itself and other applications can receive by using a Broadcast receiver. Examples of broadcasts initiated by the system are:
1. Warning that the battery is getting low
2. Screen turned off
3. Change of time zone
4. The camera has been used to take a picture
While programming, we can use Broadcast receivers to receive these broadcasted messages and behave accordingly. Applications can also initiate broadcasts. We can initiate as many broadcasts as we want and there are no limits to that.
Actually intents are not one of the Android application components, instead, it is the component activating mechanism in Android. It constitutes the core message system in Android and defines a message to activate a particular component. For example, if you want to invoke a new activity from your current activity, you need to fire an intent specifying the new activity. And if you want to start other application from your activity, then also you need to fire an intent. That is by firing an intent, you are telling the Android system to make something happen.
There are two types of Intents in Android:
In explicit Intent, we are highly specific. We specify which activity should get active on receiving the intent. These are usually used for application’s internal communications.
In implicit Intent, we are sending a message to the Android system to find a suitable Activity that can respond to the intent. For example, to send an e-mail, we can use an intent. We will also specify the data to be operated on, with the intent. On receiving the Intent, the Android system will invoke an Activity which is able to send e-mail messages with the data that we specified. If there is more than one activity is capable of receiving the Intent, the system presents a chooser to the user so that he can select which Activity/Application should handle it.
For a more comprehensive read about Android application components, read the article on the Android developer’s website here.
Stay tuned to EazyTutz for all future updates.
What do you think about the above article about Android Application Components, did I miss any points? Add your valuable comments below.
Hi, I’m Shiju P John. A Computer Science Engineering student. I run this site as my hobby and passion.