如果不采取行动的意图过滤器 [英] intent filter without action

查看:156
本文介绍了如果不采取行动的意图过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android的文件说:

Android's documentation says:

http://developer.android.com/reference/android/content/ IntentFilter.html

动作相匹配,如果任何给定的值相匹配的意图行动,或者在过滤器中指定任何动作。

"Action matches if any of the given values match the Intent action, or if no actions were specified in the filter."

我只是想对它进行测试。在我的测试应用程序,我设置这种过滤器的活动之一:

I just tried to test it. In my test application I set such filter for one of activities:

<intent-filter>
    <action android:name="ma" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="mk1" />
</intent-filter>

我试图发出这样的意图:

I try to send such intent:

Intent i = new Intent();
i.setAction("ma");
i.addCategory("mk1");
startActivity(i);

它的工作原理 - 我的活动得到启动

It works - my activity gets started.

然后我注释掉筛选器操作:

Then I comment out action in filter:

<intent-filter>
    <!-- <action android:name="ma" /> -->
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="mk1" />
</intent-filter>

我再次发出同样的意图。现在我的活动不会启动。

Again I send the very same intent. Now my activity doesn't start.

为什么呢?根据文档,当我的过滤器有没有指定的动作,用一些行动的意图应该fullfill它。

Why? According to documentation, when my filter has no actions specified, intent with some actions should fullfill it.

推荐答案

参考的在IntentFilters文档尤其是在动作试验,可以参考下面的描述中的意图分辨率

Refer to the documentation on IntentFilters especially the following description of the action test in the section on Intent Resolution:

如示例所示,当一个Intent对象名称只是一个单一的
  动作,过滤器可列出多个。该列表不能为空;一个
  过滤器必须包含至少一种元素,或者它会阻止
  所有的意图。

As the example shows, while an Intent object names just a single action, a filter may list more than one. The list cannot be empty; a filter must contain at least one element, or it will block all intents.

要通过这个测试,在意图对象必须在规定的动作
  匹配过滤器中列出的操作之一。如果对象或
  过滤器不指定一个动作,结果如下:

To pass this test, the action specified in the Intent object must match one of the actions listed in the filter. If the object or the filter does not specify an action, the results are as follows:


      
  • 如果过滤器没有列出任何动作,没有任何一个意图匹配,所以所有的意图失败的考验。没有意图能够得到
      通过过滤器。

  • If the filter fails to list any actions, there is nothing for an intent to match, so all intents fail the test. No intents can get through the filter.

在另一方面,没有指定一个动作一个Intent对象会自动通过测试 - 只要过滤器包含
  至少一个动作。

On the other hand, an Intent object that doesn't specify an action automatically passes the test — as long as the filter contains at least one action.

这是pretty从这个明显的是,的IntentFilter 包含的无操作将不匹配的任何 意图的对象。这就是你所看到的。

It is pretty clear from this that an IntentFilter that contains no actions will not match any Intent objects. Which is what you are seeing.

在另一方面,我绝对同意你的说法在文件不一致。即使我在这里复制的部分是不一致的,因为这两国都的过滤器必须至少包含一个元素,或者它会阻止所有意图的也的一个Intent对象,不指定动作自动通过测试 - 只要过滤器包含至少一个动作

On the other hand, I absolutely agree with you that the documentation is inconsistent. Even the section I copied here is inconsistent, as it states both "a filter must contain at least one element, or it will block all intents" and also "an Intent object that doesn't specify an action automatically passes the test — as long as the filter contains at least one action."

这篇关于如果不采取行动的意图过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆