The previous sections introduced the common elements of an Android application. To restate: an Android application will contain at least one Activity, Service, Broad-castReceiver, or ContentProvider. Some of these elements will advertise the Intents they are interested in processing via the IntentFilter mechanism. All of these pieces of information need to be tied together in order for an Android application to execute. The "glue" mechanism for this task of defining relationships is the AndroidManifest.xml file.
The AndroidManifest.xml file exists in the root of an application directory and contains all of the design-time relationships of a specific application and Intents. AndroidManfest.xml files act as deployment descriptors for Android applications. Listing 1.5 is an example of a very simple AndroidManifest.xml file.
Listing 1.5 AndroidManifest.xml file for a very basic Android application
<?xml version="1.0" encoding="utf-8 " ?>
package= "com.msi .manning.unlockingandroid" > <1-1
<application android:icon="@drawable/icon"> ^^ ac agename
<activity android:name=".chapterl" android:label="@string/app_name"> <<intent-filter> <-
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
< / activity> IntentFilter definition G
Application name C
Looking at this simple AndroidManifest.xml, we see that the manifest element contains the obligatory namespace as well as the Java package name O containing this application. This application contains a single Activity, with a class name of chapterl C. Note also the @string syntax. Anytime an @ symbol is used in an AndroidManifest.xml file, it is referencing information stored in one of the resource files. In this case, the label attribute is obtained from the app_name string resource defined elsewhere in the application. Resources are discussed in further detail later in chapter 3. This application's lone Activity contains a single IntentFilter definition G. The IntentFilter used here is the most common IntentFilter seen in Android applications. The action android.intent .action.MAIN indicates that this is an entry point to the application. The category android.intent.category.LAUNCHER places this Activity in the launcher window, as shown in figure 1.7. It is possible to have multiple Activity elements in a manifest file (and thereby an application), with more than one of them visible in the launcher window.
In addition to the elements used in this sample manifest file, other common tags include:
Figure 1.7 Applications are listed in the launcher based on their IntentFilter. In this example, the application "Where Do You Live" is available in the LAUNCHER category.
■ The <service> tag represents a Service. The attributes of the service tag include its class and label. A Service may also include the <intent-filter> tag.
■ The <receiver> tag represents a Broad-castReceiver, which may or may not have an explicit <intent-filter> tag.
■ The <uses-permission> tag tells Android that this application requires certain security privileges. For example, if an application requires access to the contacts on a device, it requires the following tag in its AndroidManifest.xml file:
<uses-permission android:name= "android.permission.READ_CONTACTS" / >
We revisit the AndroidManifest.xml file a number of times throughout the book because we need to add more detail for certain elements.
Now that you have a basic understanding of the Android application and the AndroidMan-ifest.xml file, which describes its components, it's time to discuss how and where it actually executes. The next section discusses the relationship between an Android application and its Linux and Dalvik virtual machine runtime.
Was this article helpful?