Android 當中的 AndroidManifest.xml 檔案

Android

簡介

使用方法

開發工具

開始寫程式

視覺化介面

視覺元件

對話元件

核心物件

事件處理

資料儲存

查詢功能

影音功能

繪圖功能

網路功能

衛星地圖

特殊功能

資源管理

裝置管理

系統核心

問題與回答

刷機升級

常用軟體

Eclipse

教學錄影

訊息

相關網站

參考文獻

最新修改

簡體版

English

在 Android 執行一個應用之前,必須先知道該應用有哪些元件 (Activity、Service 等),這些元件的描述被放在 AndroidManifest.xml 檔案中。AndroidManifest.xml 會連同執行碼與相關資源等一同打包到 .apk 專案檔中。

Before Android can start an application component, it must learn that the component exists. Therefore, applications declare their components in a manifest file that's bundled into the Android package, the .apk file that also holds the application's code, files, and resources.

AndroidManifest.xml 除了宣告元件之外,還可以定義給外部用的函式庫名稱,並且設定權限機制等。

The manifest is a structured XML file and is always named AndroidManifest.xml for all applications. It does a number of things in addition to declaring the application's components, such as naming any libraries the application needs to be linked against (besides the default Android library) and identifying any permissions the application expects to be granted.

但是 AndroidManifest.xml 的主要功能還是告訴 Android 有哪些元件,以下是一個範例。

But the principal task of the manifest is to inform Android about the application's components. For example, an activity might be declared as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest . . . >
    <application . . . >
        <activity android:name="com.example.project.FreneticActivity"
                  android:icon="@drawable/small_pic.png"
                  android:label="@string/freneticLabel" 
                  . . .  >
        </activity>
        . . .
    </application>
</manifest>

<activity> 中的 name 元素告訴 Android 哪個類別是 Activity,icon 則指定了其圖示,而 label 則指定了其顯示名稱。

The name attribute of the <activity> element names the Activity subclass that implements the activity. The icon and label attributes point to resource files containing an icon and label that can be displayed to users to represent the activity.

其他元件,像是 <service>、<receiver> 與 <provider>, 也有相似的定義方法,沒有定義在 AndroidManifest.xml 當中的類別是無法給其他應用所使用的。但是 Broadcast-Receiver 除了在 AndroidManifest.xml 中定義之外,也可以用 Context.registerReceiver() 的方式向 Android 系統註冊。

The other components are declared in a similar way — <service> elements for services, <receiver> elements for broadcast receivers, and <provider> elements for content providers. Activities, services, and content providers that are not declared in the manifest are not visible to the system and are consequently never run. However, broadcast receivers can either be declared in the manifest, or they can be created dynamically in code (as BroadcastReceiver objects) and registered with the system by calling Context.registerReceiver().

For more on how to structure a manifest file for your application, see The AndroidManifest.xml File.

Intent Filter

Intent 可以被想像成 Android 當中的事件,用來串連 Android 中的 Activity 與 Service 等元件。每當一個 Activity 想執行一個操作時,會創建出一個 intent,該 intent 描述了 Activity 想要做甚麼事、想處理什麼類型的資料等。

Android 會比較 Intent 與 AndroidManifest.xml 中的 <intent-filter> 內容,以尋找最合適的 Activity 來處理回應 Intent 的請求。

Intent 可以直接指定元件名稱,Android 會根據 AndroidManifest.xml 檔案中的宣告找出該元件並啟動之。但是如果 Intent 沒有指定對象的元件名稱,Android 會透過 <intent-filter> 去找出最適當的元件回應 Intent ,<intent-filter> 指定了元件可以回應的 Intent 類型,以下是一個範例。

An Intent object can explicitly name a target component. If it does, Android finds that component (based on the declarations in the manifest file) and activates it. But if a target is not explicitly named, Android must locate the best component to respond to the intent. It does so by comparing the Intent object to the intent filters of potential targets. A component's intent filters inform Android of the kinds of intents the component is able to handle. Like other essential information about the component, they're declared in the manifest file. Here's an extension of the previous example that adds two intent filters to the activity:

<?xml version="1.0" encoding="utf-8"?>
<manifest . . . >
    <application . . . >
        <activity android:name="com.example.project.FreneticActivity"
                  android:icon="@drawable/small_pic.png"
                  android:label="@string/freneticLabel" 
                  . . .  >
            <intent-filter . . . >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter . . . >
                <action android:name="com.example.project.BOUNCE" />
                <data android:mimeType="image/jpeg" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        . . .
    </application>
</manifest>

上述範例中的第一個 <intent-filter> 是個典型的例子,該敘述說明了 com.example.project.FreneticActivity 這個 Activity 會被放入起始功能表android.intent.category.LAUNCHER 當中。換句話說,com.example.project.FreneticActivity 這個 Activity 是該應用程式的入口點。

The first filter in the example — the combination of the action "android.intent.action.MAIN" and the category "android.intent.category.LAUNCHER" — is a common one. It marks the activity as one that should be represented in the application launcher, the screen listing applications users can launch on the device. In other words, the activity is the entry point for the application, the initial one users would see when they choose the application in the launcher.

第二個 <intent-filter> 宣告了 com.example.project.BOUNCE 這個 activity 可以運作在 image/jpeg 這種資料形態上。

The second filter declares an action that the activity can perform on a particular type of data.

一個元件可以有很多個 <intent-filter>,每個宣告不同的能力,

A component can have any number of intent filters, each one declaring a different set of capabilities. If it doesn't have any filters, it can be activated only by intents that explicitly name the component as the target.

For a broadcast receiver that's created and registered in code, the intent filter is instantiated directly as an IntentFilter object. All other filters are set up in the manifest.

For more on intent filters, see a separate document, Intents and Intent Filters.

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License