Android Manifest文件介紹

前言

開發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

action

動做標籤,這是一個很是常見的標籤,存在於intent-filter標籤中。若是與一個activity綁定,能夠表示該活動具備該動做,若是有一個意圖發出來,而且帶有這種一個Activity定義的動做,那麼,它就有可能定位到該activity,通常與category一塊兒用。web

Example:
<action android:name="android.intent.action.MAIN" />

activity

活動標籤,最經常使用的標籤,定義活動,具備大量的屬性,具體參考官方文檔瀏覽器

activity-alias

活動標籤的代替者,其最重要的屬性是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>

application

對應用程序的聲明,一樣具備大量屬性。咱們常見的Activity,Service,Receiver所有定義在此標籤內部。而且,它也有一種相似全局配置的做用,好比配置主題等。app

category

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>

compatible-screens

通常不建議用該標籤,該標籤對程序自己沒有影響,可是會影響Google Play等應用市場斷定應用程序的兼容性。單元測試

data

data也是用於Intent-filter。它提供了另一個定位activity的方式。就像網絡上一個URL地址能夠定位到網頁同樣,你能夠本身定義格式,而後跳轉到特定的activity。若是你在瀏覽器中看到一個提示:打開或安裝某某應用程序。這裏面的實現原理就是這個。我以前寫過這樣的文章測試

grant-uri-permission

這是內容提供端向客戶端提供數據所用到的一個屬性。當grandUriPermisson被設置爲true時,那麼客戶端能夠得到內容提供端的任何數據,若是設置爲false,那麼就只能訪問grant-uri-permission定義的路徑。此標籤主要用來讓你在沒有權限的狀況下,訪問其餘應用程序提供的數據。此標籤包含在Provider標籤中,語法以下:ui

<grant-uri-permission android:path="string"
                  android:pathPattern="string"
                  android:pathPrefix="string" />

instrumentation

此標籤通常是單元測試中使用,你所建立的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定義了你須要測試的應用程序。

intent-filter

定義了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()中設置。

Manifest

這個沒啥好說的,就好像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

元數據標籤,用來定義一些系統能夠用到的數據。

<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!

.

相關文章
相關標籤/搜索