Android SDK Getting Started Guide||Chapter 9 Manifest File

Chapter 9 Manifest File

So far, we are familiar with the various components in the Android project, including its resources. In today's article, we will use the project Manifest file as the core content.

For a project, the Manifest can be very simple or very complex, depending on the application. Let's first look at the components of the Manifest that are essential in all applications, and then further explore alternative components that may be involved in future project development.

Manifest files can contain many elements and attributes. We might as well click here to find detailed information about them in the Android Developer Guide. Manifest has several main functions: it specifies the application package, provides a formal description of the application components, and is also responsible for declaring permissions, necessary API levels, and link libraries. We have only discussed the most basic elements and attributes that can be listed in the Manifest, but you should also know that it can accommodate more elements and use more additional attributes outside the scope of the covered elements.

1. Manifest element

Open our project Manifest file in Eclipse - you can always find this Manifest in the root directory of the project. As mentioned earlier, you can view Manifest content in a variety of ways. In the editor area at the bottom, you can see multiple labels such as Manifest, Application, Permissions, Tools, and XML code. Now take a quick look at the tags - we need to use XML code, so switch to the "AndroidManifest.xml" tag.

9.1project_manifest_tab

The elements shown in the manifest file are generated by Eclipse when we create the project. But these are only enough to meet the needs of simple applications. In most cases, we need to add more elements to the Manifest when creating the project. The root element in the file is the manifest element:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0" >
</manifest>

Eclipse will use the package name you selected as an attribute of the manifest element when the project is created. The version code and name are initially 1 and 1.0 respectively. When you submit your app to the Play Store and make subsequent minor version updates, you need to assign an update number to each update. The version name is the actual name of the app as users see it in the Play Store, so feel free to use whatever number you like. Users cannot see the version code, and the number of the new version must be higher than the old version - but the amount of each increment is not fixed. If you try to upload a new version of your app to the Google Play Store that does not have a higher version code than the previous version, the Play Store will reject the upload.

2. Uses-SDK element

The first thing we see among the manifest elements should be the uses-sdk element:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

This element is responsible for defining the minimum necessary API level and the target level everyone sets when testing the project. We need to select these relevant values ​​when creating the application. If necessary, you can also change the properties by modifying the contents of the Manifest file after the project is created. For example, you may find that you need to use some platform features that are not available at the currently selected API level. In this case, you need to make adjustments through the Manifest file. If you change the SDK version, Eclipse will rebuild the entire project.

If the user's device is running at an API level lower than the project's minimum requirements, our application cannot be downloaded and installed. Listing the target API level means that we have tested the current application version. To ensure the reliability of your application, you should test your application at as many API levels as possible.

3. Application element

Eclipse will also insert the application element into our Manifest. This element contains several sub-elements, which we will discuss one by one later. Now let’s take a look at the content after opening the tag:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

The main properties to note are icons, labels, and topic entries. This element can also carry a variety of additional attributes. The icon attribute represents a file within the application's drawable resource. By default the project will use Android icons directly, but you can add your own icon files and reference them here. The label icon also references a string from the application resource. Open the XML file whose name contains the string "res/values". You will see the quoted string as follows:

<string name="app_name">MyFirstApp</string>

This should be a readable string as it will appear in several places in the user interface, including the device menu next to the launch icon. We usually need to change the content of the above string, such as adding spaces to the expression - that is, "My First App".

Back in the Manifest file, please pay attention to the theme attribute of the application element. It also references a resource. You can find its specific reference relationship in the XML file of the "res/values" type. Let's take a look right away. Next switch back to the Manifest tab. If you later decide to define your own application style, you can reference it in the theme properties.

4.Activity element

Among the application elements, you will see an activity element - it corresponds to the Activity class we created during project development. The activity element contains multiple sub-elements, which we will discuss in detail later. Now let’s take a look at the contents of the opened tag:

<activity
    android:name="com.example.myfirstapp.MainActivity"
    android:label="@string/app_name" >

The name attribute refers to the corresponding class using the path qualified in the application package. Labels allow us to control what is displayed in the window title when the Activity is enabled. By default, the window title is often the application name, so you generally don't need to make additional adjustments. However, as the complexity of the application increases, more activities will be added to the project - each corresponding to a set of screen display schemes in the UI. Every time you add a new Activity class to your application, you need to add a corresponding child element to the application element, as shown below:

<activity android:name=".About" >
</activity>

As you can see, we don't always need to use the full app package name. The shorthand form shown above will also work normally, as long as the Activity class (named 'About') is still in the application package defined by the manifest element. The Activity element can hold a variety of attributes that determine how users interact with it.

5. Intent filters

In the main activity element, you will see an intent-filter element:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

The Intent Filter here is used to describe which "intentions" the main activity can respond to. In the Android system, the so-called intent refers to the data object passed to the Activity when it is started. When you enable one Activity after another in your own application, you need to use the intent mechanism - but intents can also be passed between different applications.

Intent filters for the main activity indicate that they should be enabled when the application is running. The intention is to achieve this effect through the action element, which is the "MAIN" operation in the above code. Additionally, the category element is intended to describe the intent filter via the category name, which in our example is "LAUNCHER". The combination of these two elements means that our application should utilize an Activity as its main entry point, and that entry point will be launched when the application is run.

Intent filters can contain a variety of sub-elements, including data specifications. If you want to learn more, you can check out the "Intents and Intent Manager" chapter in the Android Developer Guide here. In addition to intent-filter, the activity element can also contain other sub-elements - such as metadata, which is used to define the name-value pair of a data item.

6. User permissions

Now that we've seen all the Manifest elements included in Eclipse when creating a project, let's take a look at other elements that will be involved when developing complex applications in the future. Some applications need to determine whether the user has the right to perform specific operations or view specific content. This permission-related function is implemented by the uses-permission element. When a user views our app on the Play Store, the platform displays the necessary permissions to use full functionality before the app is downloaded. If the user chooses to continue, they will need to accept the permission control prompt before the app can function properly. Permissions that must be mandatory in the Manifest include using internal data, writing to external storage, and accessing device functions such as cameras. We demonstrate this concept with the following sample code:

<uses-permission android:name="android.permission.INTERNET" />

There are several other potential permissions that applications can enforce. Please click here to view the "Manifest.permission" section of the API reference description.

7. User Equipment

There is a type of Manifest element that can be used to describe the hardware and software functions necessary for the application to run, including uses-configuration elements. In this element, you can specify requirements for navigation, keyboard, and touchscreen options. In the uses-feature element, you can list single functional requirements for hardware or software through function names and Boolean tags. These features include Bluetooth and camera options such as flash memory, location detection, and sensors. The supports-screens element allows you to define supported screen sizes for your application. The specified element can involve both size and pixel density.

8. Other elements

The elements we've discussed above mainly revolve around your initial application, but there are also elements worth paying attention to in different types of applications. The initial project we created for the standard application involved only the user menu and one or more Activity screens for the user upon launch. However, other types of apps may also include functional components and continuously running background processes - designed to handle data access or receive system notifications.

Applications usually use the Service class in the Android system to handle background processes, which requires us to add the service element in the Manifest - similar to Activity, there is a one-to-one correspondence between the service element and the Service class. The content provider in the Android application is responsible for managing access to the data source, and the specific content is included in the provider element. Finally, the receiver element in the Manifest is designed to help applications receive intents from other applications or the operating system itself.

in conclusion

So far, we have explained the most basic functions in the Manifest file of Android applications. When you create an application, Eclipse also adds the main elements necessary for the initial project to the Manifest file. As more features are introduced into the application, we need to continue to add new content to the manifest to ensure that the overall application works smoothly. If you have problems with real machine or virtual device testing during the development process, it is probably because some necessary elements are missing in the Manifest. In the next series of tutorials, we will learn how Android applications store and access data.

Guess you like

Origin blog.csdn.net/m0_69824302/article/details/132634100