close

這篇要介紹Intent和Intent filter是如何配對的

介紹如下

 

當系統收到implicit intent要啟動activity時
android系統會根據下列3個層面來找出適合的activity
action
data(URI and data type)
category

Action test
intent filter可以有0或多個<action>
如果intent filter沒有帶任何action則test會fail
intent 可以有0或多個action
intent中如果沒有帶action則會通過test(但filter內至少要有一個action)
而intent中只要有一個action符合intent filter則會成功

Category test
intent filter可以有0或多個<category>
intent可以有0或多個category
intent中的每個category都必須與filter中的符合才會通過test
其中filter內的category可以比intent的多
當intent沒帶category時則無條件通過test
其實不帶時預設會帶CATEGORY_DEFAULT
但是Activity需要有android.intent.category.DEFAULT才會收的到

Data test
intent filter可以有0或多個<Data>
每個<Data>都可由URI和MIME Type組成
intent 沒有URI 且 沒有MIME Type, 則filter沒有URI和MIME Type才會pass
intent 只有URI, 則filter也必須只有URI, 並且兩個符合才會pass
intent 只有MIME Type, 則filter也必須只有MIME Type, 且有符合的才會pass
intent有URI和MIME Type時, intent需要有URI與MIME Type,
如果intent只有URI, 則MIME Type需可明確從URI推測
而intent的URI需要與intent filter的URI相同
如果filter只有MIME Type, 會預設支援content: 與 file:
所以intent的URI有content: 與 file:則可pass

PackageManager
queryIntentActivities() 會傳回所有Activity
queryIntentServices() 可傳回類似的服務清單
queryBroadcastReceivers() 可用於廣播接收器

 

1. 官網https://developer.android.com/guide/components/intents-filters.html#Resolution

01.png  

arrow
arrow
    文章標籤
    Intent解析 Intent resolution
    全站熱搜

    RX1226 發表在 痞客邦 留言(0) 人氣()