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

AndroidManifest 標籤詳解(一):action

編輯:關於 Android 程式設計

格式

<action android:name="string" />

父標籤

<intent-filter>

描述

一個 <intent-filter> 必須包含一個或多個 <action>。若完全沒有 action,就不會有任何 Intent 能被這個 filter 攔截到——這是新手常犯的錯誤:只寫了 category 卻漏掉 action,結果元件怎麼樣都接收不到意圖。

參數

android:name

指定 action 的名稱。系統內建的標準 action 都已在 Intent 類別中定義為常數,查 API 文件即可找到清單,並以 "android.intent.action.*" 的形式賦值。例如對應 Intent.ACTION_MAIN 就寫成 android.intent.action.MAIN,對應 Intent.ACTION_VIEW 則是 android.intent.action.VIEW。

如果是自行定義的 action,建議以套件名稱作為前綴以避免與其他應用衝突:

<intent-filter>
    <action android:name="com.example.project.TRANSFORM" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

有一個細節值得注意:action 字串是不分大小寫比對的,且 filter 中的 action 比對採「包含」而非「相等」語意——意圖中的 action 只要能在 filter 清單裡找到對應項就算匹配。相對地,<data> 的比對規則嚴格得多。

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