android - Intent 类别如何匹配?

标签 android

我不明白 Android Intent 匹配的概念!我一定错过了一些东西,但我读了又读了文档,但没有明白。也许有好心人可以解释一下吗?

如果我在 list 中指定类别过滤器android.intent.category.DEFAULT,我就能够启动 Activity :

    ...
    <activity 
        android:name="mmmo.android.test.ItemDetails"
        <intent-filter>
            <action android:name="android.intent.action.INSERT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    ...

如果我不向 Intent 对象添加任何类别:

        ...
        Intent intent = new Intent(Intent.ACTION_INSERT);
        startActivity(intent);
        ...

这有效。但是,一旦我定义了 android.intent.category.DEFAULT 之外的任何其他类别,我只会收到 ActivityNotFoundException。例如。如果我指定:

    ...
    <activity 
        android:name="mmmo.android.test.ItemDetails"
        <intent-filter>
            <action android:name="android.intent.action.INSERT" />
                    <category android:name="foo.bar" />
        </intent-filter>
    </activity>
    ...

然后尝试使用以下方法启动该 Activity:

        ...
        Intent intent = new Intent(Intent.ACTION_INSERT);
        intent.addCategory("foo.bar"); 
        startActivity(intent);
        ...

这不起作用。该文档显示“... Intent 对象中的每个类别都必须与过滤器中的类别匹配。 ...”。我添加到 Intent 的类别名称与我在过滤器中指定的类别相匹配。那么为什么这不匹配并且只是抛出异常???

迈克尔

最佳答案

您还必须添加

 <category android:name="android.intent.category.DEFAULT"></category>

到 Intent 过滤器以解析 Intent 。

参见Intent :

Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity().

关于android - Intent 类别如何匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3622874/

相关文章:

java - 列出所有已安装的应用程序并将它们放入微调器中

java - 如何在 Android 中为图像设置边框?

android - 为什么 Google Cloud Endpoints 会颠倒我的参数顺序?

java - 在这种情况下如何不重复自己?安卓的java

android - 如何从在我的应用程序中打开 Activity 的网址检索变量 "?var=value"?

android - 公开分享 SKU 安全吗?

android - Google Maps android通过在 map 中移动标记来获取经纬度

android - 静态BroadcastReceiver无法正常工作

Android:删除 Html.fromHtml 的弃用警告

java - 使用混淆器时选取框工具栏标题不起作用