AndroidManifest.xml文件内容浅析

来源:互联网 发布:火四川方言版网络原唱 编辑:程序博客网 时间:2024/06/13 21:26

AndroidManifest.xml文件内容浅析

大家都知道,每个android应用程序,都离不开一个最基本的配置文件-AndroidManifest.xml。在编写Android源代码时,许多配置都需要通过修改该文件的内容来进行;程序报错时,也有可能是这个配置文件的内容出错了,所以理解该文件的内容应该是有必要的,该文件主要的功能有以下8个:

1.为java package命名,包名也作为该application的唯一标识符2.描述了应该的组件,包括activities,services,broadcast receivers以及组件的提供者。并为实现  了接口的类命名。告诉android系统有哪些组件,这些组件在哪些情况下能够启动3.确定哪些进程来管理组件4.声明了应用程序要访问被保护的API和与其他系统应用交流需要哪些特权(permissions)5.也声明了其他的程序要访问本程序组件需要的特权(permissions)6.列出了一些Instrumentation类,这些类只在开发和测试时有效,程序发布后就无效了7.声明了该应用要求的最低的android api版本8.列出了应用程序必须链接的库

下面列出了一个AndroidManifest.xml文件的模板:

<?xml version="1.0" encoding="utf-8"?><manifest>    <uses-permission />    <permission />    <permission-tree />    <permission-group />    <instrumentation />    <uses-sdk />    <uses-configuration />      <uses-feature />      <supports-screens />      <compatible-screens />      <supports-gl-texture />      <application>        <activity>            <intent-filter>                <action />                <category />                <data />            </intent-filter>            <meta-data />        </activity>        <activity-alias>            <intent-filter> . . . </intent-filter>            <meta-data />        </activity-alias>        <service>            <intent-filter> . . . </intent-filter>            <meta-data/>        </service>        <receiver>            <intent-filter> . . . </intent-filter>            <meta-data />        </receiver>        <provider>            <grant-uri-permission />            <meta-data />        </provider>        <uses-library />    </application></manifest>

下面用一个HelloWorld程序的AndroidManifest.xml文件来说明常用元素和属性的作用(只介绍了基本的元素和属性,其他元素和属性可通过末尾的官方API的URL查询):

<?xml version="1.0" encoding="utf-8"?><manifest xmlnsandroid="http://schemas.android.com/apk/res/android"    package="zcx14.nju.datapackage">    <application        androidallowBackup="true"        androidicon="@mipmap/ic_launcher"        androidlabel="@string/app_name"        androidroundIcon="@mipmap/ic_launcher_round"        androidsupportsRtl="true"        androidtheme="@style/AppTheme">        <activity androidname=".MainActivity">            <intent-filter>                <action androidname="android.intent.action.MAIN" />                <category androidname="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

解析:

<manifest>元素:是该文件的根元素。它必须包含一个<application>元素和一些特殊属性(xmls:android 和 package)。  - xmls:android属性:定义了Android命名空间,这个属性的值固定,为"http://schemas.android.com/apk    /res/android"。  - package属性:该应用的一个独一无二且完整的Java命名风格的名字。可包含大小写字母和'_'字符,其中每一个单    独的包名只 能以字母开头。  - 其他属性: android:sharedUserId, android:sharedUserLabel,android:versionCode,            android:versionNameandroid:installLocation等。  -<application>元素:见下面的<application>元素解析。

<application>元素:该元素包含了声明该程序的每个组件的子元素和对所有组件都起作用的属性声明。这些属性可能设置了对应的组件属性的默认值(可改变)和固定值(不可改变)。 - android:allowBackup属性:是否允许备份应用的数据,默认是true,当备份数据的时候,它的数据会被备份下   来。 - android:icon属性:应用的整体图标(应用程序显示在桌面上的图标),每个组件的默认图标。 - android:label属性:应用程序的名称,每个组件的默认名称。 - android:roundIcon属性:选择以圆形显示时的应用图标 - android:supportsRtl属性:Layout的布局改为从右到左 - android:theme属性:程序界面整体的主题和风格设置 - 其他属性:android:allowTaskReparenting,android:backupAgent, android:debuggable,              android:description,android:enabled,android:killAfterRestore,            android:largeHeap,android:logo,android:manageSpaceActivity,android:name,            android:permission,android:persistent,android:process,            android:restoreAnyVersion,android:taskAffinity,android:theme,android:uiOptions - <activity>元素:见下面的<activity>元素解析 - 其他元素:<activity-alias>,<service>,<receiver>,<provider>,<uses-library>

<activity>元素:声明了一个activity(一个Activity的子类),这个activity实现了部分应用界面的用户接口。所有的activities都必须在<activity>元素内声明,否则那个activity不会被系统看到也永远不会被运行。 - android:name属性:是实现了该Activity的类的名字,可用类的用全路径表示,也可以以'.'开头,如该文件中   的".MainActivity"表示,在<manifest>中声明的package下的MainActivity类。 - 其他属性:太多就不一一列出,请参考官方文档 - <intent-filter>元素:见下面的<intent-filter>

<intent-filter>元素:表明了包含它的activity,service或者broadcast可以响应的intent,必须包含<action>元素,可包含<category>和<data>元素: -<action>元素:只有android:name属性,常见的值为android.intent.action.MAIN,表示此activity是作为应用程序入   口,其他的值可以参考这个网址:   http://hi.baidu.com/linghtway/blog/item/83713cc1c2d053170ff477a7.html -<category>元素:只有android:name属性,常见的值为android.intent.category.LAUNCHER,决定应用程序是否显示在程序列  表里,其他的值可以参考这个网址:  http://chroya.javaeye.com/blog/685871

参考资料:
http://www.android-doc.com/guide/topics/manifest/category-element.html Android官方API
http://www.cnblogs.com/pilang/archive/2011/04/20/2022932.html Android学习笔记之AndroidManifest.xml文件解析

原创粉丝点击