編輯:關於 Android 程式設計
Intent 是 Android 組件之間溝通的通用消息機制,通過它可以啟動 Activity、Service,也可以發送廣播給 BroadcastReceiver。理解它的匹配規則,是寫出靈活組件的前提。
顯式意圖與隱式意圖
顯式意圖:通過 intent.setComponent()、setClassName() 或 setClass() 明確指定要激活的組件類名。目標唯一,不做任何匹配推斷。
隱式意圖:不指定組件名稱,只描述「我想做什麼」,由系統拿這份描述去和所有組件的 intent-filter 做匹配,選出合適的接收者。
一個隱式意圖的完整例子
首先在清單文件中為目標 Activity 聲明 intent-filter,可以同時聲明多個 action、多個 category 以及 data 約束:
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<activity android:label="@string/app_name" android:name=".IntentActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OtherActivity" android:label="@string/windowtitle">
<intent-filter >
<action android:name="cn.itcast.zhangxx"/>
<action android:name="cn.itcast.laoli"/>
<category android:name="cn.itcast.category.java"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="cn.itcast.category.php"/>
<data android:scheme="itcast" android:host="www.itcast.cn" android:path="/liming"/>
<data android:mimeType="image/*"/>
</intent-filter>
</activity>
</application>
然後在 Activity 中構造隱式意圖並發起跳轉:
public void openActivity(View v) {
// 只要 Intent 中的 Action 和 Category 都出現在某個 intent-filter 中,就能匹配成功
Intent intent = new Intent();
intent.setAction("cn.itcast.zhangxx");
intent.addCategory("cn.itcast.category.java");
// 注意:setData 會清空之前設置的 type,setType 也會清空之前設置的 data,
// 兩者不能分開調用,必須使用 setDataAndType
// intent.setData(Uri.parse("itcast://www.itcast.cn/liming"));
// intent.setType("image/jpeg");
intent.setDataAndType(Uri.parse("itcast://www.itcast.cn/liming"), "image/jpeg");
startActivity(intent); // 該方法會自動為 Intent 添加 CATEGORY_DEFAULT
}
匹配規則:三個測試維度
intent-filter 的作用就是拿來匹配隱式 Intent 的。匹配時只考察三個方面:動作(Action)、數據(Data,含 URI 與 MIME 類型)、類別(Category)。三者全部通過才算匹配成功。
一、動作測試
一個 Intent 對象最多只能指定一個動作名稱,而一個 filter 可以列舉多個。兩者的匹配規則是:
如果 filter 沒有聲明任何 action,那麼它會阻塞所有意圖,任何 Intent 都無法通過;反之,只要 filter 至少聲明了一個 action,那麼沒有指定 action 的 Intent 就能自動通過這一項測試。
二、類別測試
Intent 中的每一個 category 都必須能在 filter 中找到對應項,filter 可以額外多列一些,但不能缺少 Intent 中已有的任何一個。
原則上沒有 category 的 Intent 應該总能通過測試,但這裡有一個重要例外:Android 把所有傳給 startActivity() 的隱式意圖都視為至少包含 android.intent.category.DEFAULT。因此,想要接收隱式意圖的 Activity 必須在自己的 intent-filter 中顯式聲明 CATEGORY_DEFAULT。唯一的例外是同時帶有 android.intent.action.MAIN 和 android.intent.category.LAUNCHER 的 filter——它們是啟動器的入口,不需要額外聲明 DEFAULT。
三、數據測試
URI 的比較是分段進行的:filter 只聲明 scheme,則所有該 scheme 的 URI 都能匹配;聲明了 scheme 和 host 但沒有 path,則所有同 scheme 同 host 的 URI 都能匹配,不論路徑為何;只有 scheme、host、path 全部聲明時,才要求三者完全一致。此外 path 支持通配符,可以實現部分匹配。
具體規則可以歸納為四條:
a. 既不帶 URI 也不帶類型的 Intent,只有在 filter 同樣未指定 URI 和類型時才能通過。
b. 只帶 URI 不帶類型的 Intent(如 mailto:、tel: 這類並不引用實際數據的 URI),只有在 filter 也未指定類型、且 URI 能匹配時才能通過。
c. 只帶類型不帶 URI 的 Intent,只有在 filter 聲明了相同類型且未指定 URI 時才能通過。
d. 同時帶 URI 和類型(或類型可從 URI 推斷)的 Intent,只要類型與 filter 中列出的類型一致即可通過;如果 URI 也能與 filter 中某個 URI 匹配,或者它使用 content:/file: scheme 而 filter 未指定 URI,同樣可以通過。換句話說,一個只列舉了數據類型而沒指定 URI 的 filter,被認為同時支持 content: 和 file: 兩種數據來源。
把這三個維度的規則記牢,再遇到「明明寫了 intent-filter 卻跳不過去」的問題時,就能快速定位到底是 action、category 還是 data 哪一項沒對上。實戰中最常見的原因,恰恰就是漏掉了 CATEGORY_DEFAULT。
手機QQ聊天記錄怎麼導出?檔案位置與備份路徑完整說明
聊天記錄裡偶爾會藏著重要資訊:一段約定時間的對話、一組別人傳給你的帳號、一張需要留存的截圖。手機QQ內建了導出功能,但入口藏在聊天記錄頁面裡,很多人不知道。此外,直接從檔
Android MVP 模式實戰:從觀念到代碼的完整演練
概要 MVP 模式大家都聽過,它是由 MVC 演變而來的。先回顧一下 MVC:Model 存放實體模型與業務邏輯,View 存放布局與資源文件,Control 則對應 A
Android 開機自啟動實作教學
開機自啟動的核心是接收 BOOT_COMPLETED 廣播。應用必須宣告 RECEIVE_BOOT_COMPLETED 權限,並在清單中註冊 BroadcastRecei
Android 自訂 View 實作閃耀文字效果
想讓 Android 上的文字不只是靜態顯示,而是帶有流動光澤,可以透過自訂 View 搭配漸層與矩陣位移來完成。這種效果常用在活動頁、會員專區或需要強調視覺焦點的區塊。