关于intent-filter的误区

相信大家都看过官方文档中如下的一段(http://developer.android.com/guide/components/intents-filters.html):

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值