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

Android 隱式意圖配置:Intent-filter 解析

編輯:關於 Android 開發

Intent-filter 的作用

當系統收到隱式 Intent,會依 action、category、data 比對各元件宣告的 intent-filter,找出可處理的 Activity、Service 或 BroadcastReceiver。

動作測試

intent-filter 可包含多個 action。Intent 只要符合其中一項 action,即可通過動作測試。

類別測試

category 用於描述元件可服務的情境。Intent 中所有 category 都必須與 filter 中至少一組 category 完全匹配。

資料測試

data 可指定 scheme、host、port、path、mimeType。URI 與型別需與 filter 宣告相符。

實作範例

建立自訂 action 與 data,並在目標 Activity 的 intent-filter 中接收:

Intent intent = new Intent();
intent.setAction("com.example.ACTION_VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.parse("example://data"), "vnd.android.cursor.item/example");
startActivity(intent);

目標 Activity 可在 onCreate 中以 getIntent().getData() 取得 URI。

清單檔中需為目標 Activity 加入對應 action、category 與 data 設定,才能被系統匹配。

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