关于intent-filter的误区
生活随笔
收集整理的這篇文章主要介紹了
关于intent-filter的误区
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
相信大家都看過官方文檔中如下的一段(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>
?
事實情況如何呢?的確是官方文檔描述的不嚴謹,以至于看起來前后矛盾了(inconsistent)。 這里對官方文檔中的描述做下補充。 首先,這一句話:“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)”是有爭議的,并且看起來和However之前的描述自相矛盾,分三點來說明這個However: 1. ?如果action只和category組合使用(隱式調用的條件),那么這個However是不對的,intent中不指定action,那么無法啟動目標組件(一般為Activity);
2. ?如果action和category、data組合使用,那么這個However是對的,如下代碼可以啟動目標組件,盡管intent中沒有指定action:
?3. ?我們可以看出,在intent-filter中的action、category和data中,action是主要的,而category和data算是輔助的,但是上面的一個However制造出了一個特殊的場景,那就是“intent-filter中列出了action,但是intent中沒有指定action,然而卻匹配成功了”,這看起來又和“To get through this filter, the action specified in the?Intent?must match one of the actions listed in the filter.”這句話矛盾了。
結語:官方文檔仍然需要抱著懷疑的態度去看。向如下錯誤,我就不提了,硬把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).
關于這一段描述,讀起來好像是含糊不清甚至自相矛盾的,而國內一些翻譯后的文章也就直接翻譯了,結果就是翻譯者和讀者理解起來都含糊不清。而stackoverflow上也有人對此提出了質疑,如下所示:?
事實情況如何呢?的確是官方文檔描述的不嚴謹,以至于看起來前后矛盾了(inconsistent)。 這里對官方文檔中的描述做下補充。 首先,這一句話:“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)”是有爭議的,并且看起來和However之前的描述自相矛盾,分三點來說明這個However: 1. ?如果action只和category組合使用(隱式調用的條件),那么這個However是不對的,intent中不指定action,那么無法啟動目標組件(一般為Activity);
2. ?如果action和category、data組合使用,那么這個However是對的,如下代碼可以啟動目標組件,盡管intent中沒有指定action:
?3. ?我們可以看出,在intent-filter中的action、category和data中,action是主要的,而category和data算是輔助的,但是上面的一個However制造出了一個特殊的場景,那就是“intent-filter中列出了action,但是intent中沒有指定action,然而卻匹配成功了”,這看起來又和“To get through this filter, the action specified in the?Intent?must match one of the actions listed in the filter.”這句話矛盾了。
結語:官方文檔仍然需要抱著懷疑的態度去看。向如下錯誤,我就不提了,硬把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
總結
以上是生活随笔為你收集整理的关于intent-filter的误区的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android进程使用Messenger
- 下一篇: AS编写sdk并打成jar包供其它APP