Android’s 4 Most Common Components
In iOS and Windows Phone, all applications need a basic starter code. This is not the case in Android. In Android, you just need to declare the main entry point into the application in the manifest file. An application entry point is a class with a particular declaration. Each class expected to interact with the user is known as an activity. Below is an excerpt from the manifest file where the main activity is declared.
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
An Android application results from the combination of up to four different types of components and below are the most common. But first, allow me define some Android-specific terminology…
- An intent is a data structure that carries information about the action to perform, a message sent to target components such as an activity or a background service.
- An intent filter is the list of intents that a target component, (i.e., an activity), can handle
- MAIN is a standard Android action that is sent to start a component.
- LAUNCHER is an attribute that causes the entry point to be listed in the application launcher pad with its own icon and label. An activity serving as the main entry point in the application must be able to support both, the MAIN action and the LAUNCHER category.
1. Activities: An activity should provide a screen for the user to interact with. Typically, an activity hosts a view, which can be an application-specific view or a system-provided view. An activity is associated with a window and usually (but not necessarily always) covers the entire screen. An application needs to have at least one main activity to start but can have a number of other activities—roughly one per screen. Activities are organized in a stack and when a new activity starts, it goes to the top of the stack. Users can then navigate through the stack using the Back button. Activities receive notification from the system about relevant facts during their lifetime. For example, activities can be notified when they are pushed to the background and restored.
2. Services: A service is a UI-less component that runs in the background, usually engaged in the performance of long-running tasks such as uploading or downloading data and application-specific calculations. In general, a service is used to perform any task that is asynchronous with respect to the main user interface. A service is normally started by an activity.
3. Broadcast Receivers: A broadcast receiver listens to special messages being broadcast by the system or individual applications. An interesting example of a broadcast message is a network-state-change that is sent out when you get or lose connectivity. You get similar messages when your connectivity changes from WiFi to 3G, the battery is dangerously low, or an SMS message is received. Broadcast is not limited to the system, applications can broadcast as well. A receiver is often UI-less but can display notifications on the status bar. A receiver is usually quite a simple component and relays work to other services or activities.
4. Content Providers: Finally, a content provider is a component that manages shared data and optionally exposes query and update capabilities for other components to invoke. An example is the provider for data about the contacts you have stored in the phone. One piece of information that you often need to add to a manifest file is the list of permissions