开发Android程序的人都知道,Andorid程序必定有一个AndroidManifest文件。这个文件会告诉Android系统你本身app的信息,在运行你的app以前系统必须了解的信息。而且,你必需要在Android Manifest文件定义一些信息,好比这个程序的运行环境,app所须要的权限,app所包含四大组件的内容,等等。AndoridManifest文件的结构通常以下:html
<?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 /> <path-permission /> </provider> <uses-library /> </application> </manifest>
下面咱们就一次来看看各标签所表达的意思。android
动做标签,这是一个很是常见的标签,存在于intent-filter标签中。若是与一个activity绑定,能够表示该活动具备该动做,若是有一个意图发出来,而且带有这种一个Activity定义的动做,那么,它就有可能定位到该activity,通常与category一块儿用。web
Example: <action android:name="android.intent.action.MAIN" />
活动标签,最经常使用的标签,定义活动,具备大量的属性,具体参考官方文档。浏览器
活动标签的代替者,其最重要的属性是android:targetActivity,经过这个属性能够设定它指向的activity。通常能够理解为活动的别名。若是你向下面同样定义的话,你就能够得到两个程序的入口网络
<activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity-alias android:name="MainActivityAlias" android:targetActivity="MainActivity" android:label="MainActivityAlias" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias>
对应用程序的声明,一样具备大量属性。咱们常见的Activity,Service,Receiver所有定义在此标签内部。而且,它也有一种相似全局配置的做用,好比配置主题等。app
category标签通常与Action合用,它也是用来定位activity的,当action不能够肯定惟一的activity时,这时候为Intent-filter添加category标签就能够进一步肯定是哪个activity,一个Intent-filter能够有多个activity。ide
Example: <activity android:name=".MainActivity" android:theme="@android:style/Theme.NoDisplay" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
通常不建议用该标签,该标签对程序自己没有影响,可是会影响Google Play等应用市场断定应用程序的兼容性。单元测试
data也是用于Intent-filter。它提供了另一个定位activity的方式。就像网络上一个URL地址能够定位到网页同样,你能够本身定义格式,而后跳转到特定的activity。若是你在浏览器中看到一个提示:打开或安装某某应用程序。这里面的实现原理就是这个。我以前写过这样的文章。测试
这是内容提供端向客户端提供数据所用到的一个属性。当grandUriPermisson被设置为true时,那么客户端能够得到内容提供端的任何数据,若是设置为false,那么就只能访问grant-uri-permission定义的路径。此标签主要用来让你在没有权限的状况下,访问其余应用程序提供的数据。此标签包含在Provider标签中,语法以下:ui
<grant-uri-permission android:path="string" android:pathPattern="string" android:pathPrefix="string" />
此标签通常是单元测试中使用,你所建立的Instrumentation类能够监测你应用程序的交互,而且它会在你程序的任何组件以前被初始化。
SYNTAX: <instrumentation android:functionalTest=["true" | "false"] android:handleProfiling=["true" | "false"] android:icon="drawable resource" android:label="string resource" android:name="string" android:targetPackage="string" />
functionalTest属性决定它是否用于单元测试,handleProfiling决定是否开启分析功能,andorid:name则是你本身定义的Instrumentation子类,targetPackgage定义了你须要测试的应用程序。
定义了Activity,Service或者广播能够接收到的Intent.也是最经常使用的标签之一。必须包含action标签。
SYNTAX: <intent-filter android:icon="drawable resource" android:label="string resource" android:priority="integer" > . . . </intent-filter> android:priority用来设置优先级。取值范围为-1000到1000,默认为0。通时这一能够在IntentFilter中的setPriority()中设置。
这个没啥好说的,就好像web的html标签同样,不过在Manifest文件里能够定义不少信息。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="string" android:sharedUserId="string" android:sharedUserLabel="string resource" android:versionCode="integer" android:versionName="string" android:installLocation=["auto" | "internalOnly" | "preferExternal"] > . . . </manifest>
元数据标签,用来定义一些系统能够用到的数据。
<meta-data android:name="string" android:resource="resource specification" android:value="string" />
你必须为元数据定义一个独一无二的名字,好让程序能够得到它的值,若是是一个字符串的话,通常能够这样定义:
<meta-data android:name="com.fyales.name" android:value="fyales" />
若是是图片的话,通常就用到了android:resource属性
<meta-data android:name="com.fyales.picture" android:value="@drawable/ic_launch" />
以后再activity中进行调用:
ActivityInfo info = this.getPackageManager().getActivityInfo(getComponentName(),PackageManager.GET_META_DATA);
info.metaData.getString("com.fyales.picture");
这里须要注意的是,你meta-data定义的位置不一样,那么你获取这个元数据的方法也不一样,上面个人meat-data是定义在activity中的,若是定义在Application中,就应该像下面同样调用,以此类推:
ApplicationInfo appInfo = this.getPackageManager() .getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA); appInfo.metaData.getString("meta_name");
Android Manifest在谷歌官网有详细讲解,有须要的同窗也能够直接访问讲解。里面有标签的完整讲解。若是这篇文章有什么疏漏,欢迎拍砖。
以上:
口号:Make things interesting!
.