Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Intent 與 IntentFilter 完全解析:屬性、篩選規則與匹配機制

編輯:關於 Android 程式設計

Intent 不只是啟動 Activity、Service、BroadcastReceiver 這三類元件的鑰匙,同時也是元件之間溝通的重要媒介。

為什麼要用 Intent 啟動元件

  1. 一致的程式模型:無論目標是 Activity、Service 還是 BroadcastReceiver,都能用同一套 Intent 封裝啟動意圖。
  2. 高層次解耦:有些時候應用只想啟動「具備某種能力」的元件,而不願與特定實作耦合,Intent 正好能做到這件事。

Component 屬性:顯式 Intent

setComponent(ComponentName comp) 用於指定目標元件。ComponentName 的三個建構子分別接受 (String pkg, String cls)、(Context pkg, String cls)、(Context pkg, Class<?> cls)——也就是包名加類名,足以唯一確定一個元件。

ComponentName comp = new ComponentName(FirstActivity.this, SecondActivity.class);
Intent intent = new Intent();
intent.setComponent(comp);

// 完全等價於
Intent intent2 = new Intent(FirstActivity.this, SecondActivity.class);

此外也可用 setClass()、setClassName() 顯式指定,用 getComponent() 取回 ComponentName。這種寫法屬於顯式 Intent,多半用於啟動本應用的元件,因為必須事先知道目標類別的完整名稱。一旦指定了 ComponentName,Intent 的其他屬性都會被忽略。

Action 屬性:一句話描述動作

action 是代表某種動作的字串,由 setAction() 設定、getAction() 讀取。Intent 類預定義了許多常量,例如:

  • ACTION_CALL:撥號(目標為 Activity)
  • ACTION_EDIT:向使用者顯示資料供編輯
  • ACTION_MAIN:作為 task 的初始 Activity 啟動
  • ACTION_BATTERY_LOW:電量過低提醒(目標為 BroadcastReceiver)
  • ACTION_SCREEN_ON:螢幕開啟

自訂 action 建議以應用包名作為前綴再接續大寫字串,例如 cn.xing.upload.action.UPLOAD_COMPLETE,這才是命名良好的做法。

Category 屬性:額外的匹配條件

category 同樣是字串,用來補充目標元件必須滿足的條件,一個 Intent 可以帶多個。以 addCategory() 加入、getCategories() 讀取。常見者:

  • CATEGORY_HOME:目標 Activity 必須是顯示 homescreen 的那一個
  • CATEGORY_LAUNCHER:可作為 task 棧的初始 Activity,常與 ACTION_MAIN 搭配
  • CATEGORY_GADGET:可被嵌入其他 Activity 作為其中一部分

Data 屬性:操作對象的 URI

data 指定所操作資料的 URI,常與 action 搭配:action 為 ACTION_EDIT 時 data 指向被編輯文件的 URI;ACTION_CALL 時是以 tel: 開頭並附加號碼的 URI;ACTION_VIEW 時則是以 http: 開頭的網址。設定方法:setData() 設 URI、setType() 設 MIME 類型、setDataAndType() 同時設定兩者。

Extra 屬性:捎帶的附加資料

啟動元件時往往需要帶點額外資料,用 putExtra() 完成。它有多組重載,可承載基本型別及其陣列、String 及其陣列、Serializable、Parcelable、Bundle 等。Bundle 與 Map 類似,用於存放鍵值對。

Flag 屬性:告訴系統怎麼啟動

  • FLAG_ACTIVITY_NEW_TASK:把目標 Activity 作為新 task 的初始 Activity
  • FLAG_ACTIVITY_NO_HISTORY:不將目標 Activity 放入歷史堆疊
  • FLAG_FROM_BACKGROUND:標示這個 Intent 來自考背景操作而非使用者直接選擇

IntentFilter:元件的自我推薦

多數元件都會宣告一個或多個 IntentFilter,表明自己能處理哪些 Intent,通常寫在 AndroidManifest.xml 中,於 <activity>、<receiver>、<service> 元素內加入 <intent-filter> 子元素:

<!-- 宣告為程式入口的 Activity -->
<activity android:name=".FirstActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

隱式 Intent 的三道測試

系統處理隱式 Intent 時,會拿 Intent 與各 IntentFilter 的 action、data、category 逐一比對,三項全中才有資格成為候選;若有多個符合,再由使用者挑選。

1. action 測試

Intent 最多一個 action,filter 可有多個。通過條件是「filter 定義了 intent 的 action」。若 filter 完全沒定義 action,它會阻擋所有 intent;若 intent 沒定義 action,則只要 filter 有定義即可通過。

2. category 測試

兩側都可以有多個 category,通過條件是「filter 定義了 intent 的所有 category」。因此 intent 若沒有 category,所有 filter 都能通過。但有個重要例外:以 startActivity() 啟動時,系統會自動補上一個 android.intent.category.DEFAULT,這意味著除了程式入口之外,任何期望被隱式啟動的 Activity 都必須在自己的 filter 中宣告 DEFAULT。

3. data 測試

Intent 最多一個 data,filter 可有多個。規則如下:

  • intent 未指定 data 與 type:只有同样未定義 data 與 type 的 filter 能通過。
  • intent 有 data、無 type:只有定義相同 data 且未定義 type 的 filter 能通過。
  • intent 無 data、有 type:只有未定義 data 且定義相同 type 的 filter 能通過。
  • intent 兩者皆有:必須 data 與 type 都相同。

data 是 URI,含 scheme、host、port、path,形式為 scheme://host:port/path,四者皆為可選。比較時只比 filter 中有宣告的部分——若 filter 只寫了 scheme,那就只比 scheme,相同即視為「相同的 data」。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved