关于intent-filter的误区
生活随笔
收集整理的這篇文章主要介紹了
关于intent-filter的误区
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
相信大家都看過(guò)官方文檔中如下的一段(http://developer.android.com/guide/components/intents-filters.html):
? ??<action?android:name="android.intent.action.EDIT"?/>
? ??<action?android:name="android.intent.action.VIEW"?/>
? ? ...
</intent-filter>
?
事實(shí)情況如何呢?的確是官方文檔描述的不嚴(yán)謹(jǐn),以至于看起來(lái)前后矛盾了(inconsistent)。 這里對(duì)官方文檔中的描述做下補(bǔ)充。 首先,這一句話:“To get through this filter, the action specified in the?Intent?must match one of the actions listed in the filter. If the filter does not list any actions, there is nothing for an intent to match, so all intents fail the test.?”是完全正確的,但是這個(gè)“However, if an?Intent?does not specify an action, it will pass the test (as long as the filter contains at least one action)”是有爭(zhēng)議的,并且看起來(lái)和However之前的描述自相矛盾,分三點(diǎn)來(lái)說(shuō)明這個(gè)However: 1. ?如果action只和category組合使用(隱式調(diào)用的條件),那么這個(gè)However是不對(duì)的,intent中不指定action,那么無(wú)法啟動(dòng)目標(biāo)組件(一般為Activity);
2. ?如果action和category、data組合使用,那么這個(gè)However是對(duì)的,如下代碼可以啟動(dòng)目標(biāo)組件,盡管intent中沒(méi)有指定action:
?3. ?我們可以看出,在intent-filter中的action、category和data中,action是主要的,而category和data算是輔助的,但是上面的一個(gè)However制造出了一個(gè)特殊的場(chǎng)景,那就是“intent-filter中列出了action,但是intent中沒(méi)有指定action,然而卻匹配成功了”,這看起來(lái)又和“To get through this filter, the action specified in the?Intent?must match one of the actions listed in the filter.”這句話矛盾了。
結(jié)語(yǔ):官方文檔仍然需要抱著懷疑的態(tài)度去看。向如下錯(cuò)誤,我就不提了,硬把restart寫成repeat。 SYNTAX: <set
??android:ordering=["together"?|?"sequentially"]>
? ??<objectAnimator
? ? ? ??android:propertyName="string"
? ? ? ??android:duration="int"
? ? ? ??android:valueFrom="float?|?int?|?color"
? ? ? ??android:valueTo="float?|?int?|?color"
? ? ? ??android:startOffset="int"
? ? ? ??android:repeatCount="int"
? ? ? ??android:repeatMode=["repeat"?|?"reverse"]
? ? ? ??android:valueType=["intType"?|?"floatType"]/>
? ??<animator
? ? ? ??android:duration="int"
? ? ? ??android:valueFrom="float?|?int?|?color"
? ? ? ??android:valueTo="float?|?int?|?color"
? ? ? ??android:startOffset="int"
? ? ? ??android:repeatCount="int"
? ? ? ??android:repeatMode=["repeat"?|?"reverse"]
? ? ? ??android:valueType=["intType"?|?"floatType"]/>
? ??<set>
? ? ? ? ...
? ??</set>
Action test
To specify accepted intent actions, an intent filter can declare zero or more?<action>?elements. For example:
<intent-filter>? ??<action?android:name="android.intent.action.EDIT"?/>
? ??<action?android:name="android.intent.action.VIEW"?/>
? ? ...
</intent-filter>
To get through this filter, the action specified in the?Intent?must match one of the actions listed in the filter.
If the filter does not list any actions, there is nothing for an intent to match, so all intents fail the test. However, if an?Intent?does not specify an action, it will pass the test (as long as the filter contains at least one action).
關(guān)于這一段描述,讀起來(lái)好像是含糊不清甚至自相矛盾的,而國(guó)內(nèi)一些翻譯后的文章也就直接翻譯了,結(jié)果就是翻譯者和讀者理解起來(lái)都含糊不清。而stackoverflow上也有人對(duì)此提出了質(zhì)疑,如下所示:?
事實(shí)情況如何呢?的確是官方文檔描述的不嚴(yán)謹(jǐn),以至于看起來(lái)前后矛盾了(inconsistent)。 這里對(duì)官方文檔中的描述做下補(bǔ)充。 首先,這一句話:“To get through this filter, the action specified in the?Intent?must match one of the actions listed in the filter. If the filter does not list any actions, there is nothing for an intent to match, so all intents fail the test.?”是完全正確的,但是這個(gè)“However, if an?Intent?does not specify an action, it will pass the test (as long as the filter contains at least one action)”是有爭(zhēng)議的,并且看起來(lái)和However之前的描述自相矛盾,分三點(diǎn)來(lái)說(shuō)明這個(gè)However: 1. ?如果action只和category組合使用(隱式調(diào)用的條件),那么這個(gè)However是不對(duì)的,intent中不指定action,那么無(wú)法啟動(dòng)目標(biāo)組件(一般為Activity);
2. ?如果action和category、data組合使用,那么這個(gè)However是對(duì)的,如下代碼可以啟動(dòng)目標(biāo)組件,盡管intent中沒(méi)有指定action:
?3. ?我們可以看出,在intent-filter中的action、category和data中,action是主要的,而category和data算是輔助的,但是上面的一個(gè)However制造出了一個(gè)特殊的場(chǎng)景,那就是“intent-filter中列出了action,但是intent中沒(méi)有指定action,然而卻匹配成功了”,這看起來(lái)又和“To get through this filter, the action specified in the?Intent?must match one of the actions listed in the filter.”這句話矛盾了。
結(jié)語(yǔ):官方文檔仍然需要抱著懷疑的態(tài)度去看。向如下錯(cuò)誤,我就不提了,硬把restart寫成repeat。 SYNTAX: <set
??android:ordering=["together"?|?"sequentially"]>
? ??<objectAnimator
? ? ? ??android:propertyName="string"
? ? ? ??android:duration="int"
? ? ? ??android:valueFrom="float?|?int?|?color"
? ? ? ??android:valueTo="float?|?int?|?color"
? ? ? ??android:startOffset="int"
? ? ? ??android:repeatCount="int"
? ? ? ??android:repeatMode=["repeat"?|?"reverse"]
? ? ? ??android:valueType=["intType"?|?"floatType"]/>
? ??<animator
? ? ? ??android:duration="int"
? ? ? ??android:valueFrom="float?|?int?|?color"
? ? ? ??android:valueTo="float?|?int?|?color"
? ? ? ??android:startOffset="int"
? ? ? ??android:repeatCount="int"
? ? ? ??android:repeatMode=["repeat"?|?"reverse"]
? ? ? ??android:valueType=["intType"?|?"floatType"]/>
? ??<set>
? ? ? ? ...
? ??</set>
</set>
原文地址:?http://weibo.com/p/1001603911234926855853?from=page_100505_profile&wvr=6&mod=wenzhangmod
總結(jié)
以上是生活随笔為你收集整理的关于intent-filter的误区的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android进程使用Messenger
- 下一篇: AS编写sdk并打成jar包供其它APP