有目的地启动活动 [英] Launching an Activity with an Intent

查看:81
本文介绍了有目的地启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的AndroidManifest中包含以下内容:

I have the following in my AndroidManifest:

<activity android:name="IntentChild"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.EDIT" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="vnd.intent.cursor.item/intent_example"
              android:host="example.intent"
              android:path="intent_example"
              android:scheme="content"
        />
    </intent-filter>
</activity>

我通过

Uri uri = new Uri.Builder().scheme("content").authority("example.intent").appendPath("intent_example").build(); 
Intent intent = new Intent(Intent.ACTION_EDIT, uri); 
IntentExample.this.startActivity(intent);

但是我得到:

E/AndroidRuntime( 865): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.EDIT dat=content:// 
example.intent/intent_example }

我做错了什么?另外,Uri.Builder.authority()是否与清单中的<data>标记的android:host属性引用相同的内容?

What am I doing wrong? Also, does Uri.Builder.authority() refer to the same thing as the android:host attribute of the <data> tag in my manifest?

推荐答案

在@ A--C的评论提示下,我最终添加了对Intent.setType()的调用以设置所需的MIME类型:

Prompted by the comment by @A--C, I ended up adding a call to Intent.setType() to set the desired MIME type:

Uri uri = new Uri.Builder().scheme("content").authority("example.intent").appendPath("intent_example").build(); 
Intent intent = new Intent(Intent.ACTION_EDIT, uri); 
intent.setType("vnd.intent.cursor.item/intent_example");
IntentExample.this.startActivity(intent);

为简单起见,我还修剪了<intent-filter>以仅声明android:mimeType.我猜,但不能完全肯定,这并不像以前的更改那么重要.

For simplicity, I also pruned my <intent-filter> to only declare the android:mimeType. I guess, but not entirely certain, that this isn't as important as the previous change.

<activity android:name="IntentChild"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.EDIT" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="vnd.intent.cursor.item/intent_example"/>
    </intent-filter>
</activity>

这篇关于有目的地启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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