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

Android 隱式啟動完全指南:action、category 與 data 的匹配規則

編輯:關於 Android 程式設計

簡介

在 Android 中,啟動 Activity、Service 或 BroadcastReceiver 有兩種途徑:顯式啟動與隱式啟動。以下以 Activity 為例說明。

顯式啟動直接指定上下文與目標類別:

Intent intent = new Intent(this, Activity.class);
startActivity(intent);

隱式啟動則靈活得多,它透過 action、category 與 data 的組合,交由系統去比對並挑選合適的元件。

特別提醒:以隱式方式啟動的 Activity,在 AndroidManifest 中務必加上
<category android:name="android.intent.category.DEFAULT" />

一、以 action 與 category 匹配

透過 Intent 設定 action 與 category,讓系統找出對應的 Activity。

AndroidManifest.xml:

<intent-filter>
    <action android:name="me.pwcong.demo.activity" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.DEMO" />
</intent-filter>

程式碼:

Intent intent = new Intent();
// 指定 AndroidManifest 中 action 標籤的 name 值
intent.setAction("me.pwcong.demo.activity");
// 可不加上 category,系統預設會帶入 Intent.CATEGORY_DEFAULT
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addCategory("android.intent.category.DEMO");
startActivity(intent);

要點

若有多個 Activity 在 Manifest 中宣告了相同的 action 與 category,執行啟動時系統會跳出選擇清單,讓使用者決定要開啟哪一個。

單靠 category 無法完成匹配,必須搭配 action。原因是當 filter 中只有 category 時,系統僅以 category 進行比對,往往找不到目標而直接報錯。例如:

<intent-filter>
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="me.pwcong.demo.hello" />
</intent-filter>
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addCategory("me.pwcong.demo.hello");
startActivity(intent);

二、以 data 匹配

如果 Manifest 中宣告了 data,那麼程式碼端就必須設定 Intent 的 data,例如 intent.setData(uri)。

AndroidManifest.xml:

<intent-filter>
    <action android:name="me.pwcong.demo.data" />
    <category android:name="android.intent.category.DEFAULT" />
    <data
        android:scheme="content"
        android:host="pwcong.me"
        android:port="8080"
        android:path="/hello" />
</intent-filter>

程式碼:

Uri uri = Uri.parse("content://pwcong.me:8080/hello");
Intent intent = new Intent();
intent.setAction("me.pwcong.demo.data");
// 可不加上 category,系統預設會帶入 Intent.CATEGORY_DEFAULT
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(uri);
startActivity(intent);

要點

data 的語法:

<data android:host="string"
      android:mimetype="string"
      android:path="string"
      android:pathPattern="string"
      android:pathPrefix="string"
      android:port="string"
      android:scheme="string" />

Uri 的組成形式:scheme://host:port/path(亦可用 pathPrefix 或 pathPattern)。比對規則如下:

1. 若未指定 scheme,其餘屬性全部失效;

2. 若未指定 host,則 port、path、pathPrefix、pathPattern 全部失效;

3. 一個 intent-filter 中可宣告多組 data,只要比對上其中一組即可。

三、以 action 與 mimeType 匹配

intent-filter 中的 mimeType 屬性用來告知系統「本 Activity 能處理哪一類檔案」。例如設為 text/plain 代表可處理 .txt 檔。

常見 mimeType 對照(節錄):

3gp   video/3gpp
avi   video/x-msvideo
bmp   image/bmp
bz2   application/x-bzip2
css   text/css
doc   application/msword
gif   image/gif
gz    application/x-gzip
htm   text/html
html  text/html
ico   image/x-icon
jar   application/java-archive
jpe   image/jpeg
jpeg  image/jpeg
jpg   image/jpeg
js    application/x-javascript
m3u   audio/x-mpegurl
mid   audio/midi
midi  audio/midi
mov   video/quicktime
mp3   audio/mpeg
mp4   video/mp4
mpeg  video/mpeg
mpg   video/mpeg
pdf   application/pdf
png   image/png
ppt   application/vnd.ms-powerpoint
rar   application/x-rar-compressed
rtf   application/rtf
swf   application/x-shockwave-flash
tar   application/x-tar
tgz   application/x-tar
tif   image/tiff
tiff  image/tiff
txt   text/plain
wav   audio/x-wav
wbmp  image/vnd.wap.wbmp
wml   text/vnd.wap.wml
xml   text/xml
xsl   text/xml
zip   application/zip

完整列表可參考 IANA 與各平台官方文件,實務上只要記住常用的圖片、音訊、影片、文件與網頁類型即可應付大部分情境。

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