使用应用程序之间的隐含意图自定义操作自定义、意图、应用程序、操作

2023-09-07 16:27:48 作者:别用你吻过她的唇说爱我

我一直在试图让两个独立的应用程序的两项活动使用自定义操作和一个隐含的意图进行沟通。

I have been trying to get two activities in two separate applications to communicate using a custom action and an implicit intent.

第一个应用程序(服务器),具有以下明显的:

The first application (server), has the following manifest:

<application android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" android:theme="@style/AppTheme">
    <activity android:name="edu.example.sharing.manager.SecureFileShare"
        android:label="@string/title_activity_secure_file_share" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="edu.example.sharing.action.STORE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>
    </activity>
</application>

第二个应用程序创建一个意图如下:

The second application creates an intent as follows:

File f = new File(s);
Uri fileUri = Uri.fromFile(f);
Intent intent = new Intent();
intent.setData(fileUri);
intent.setAction("edu.example.sharing.action.STORE");               
startActivityForResult(intent, STORE_REQUEST);

其表现是正常的。当我尝试发送意向客户端应用程序,但是,我得到一个未发现异常活动:

Its manifest is normal. When I try to send the intent in the client application, however, I get an activity not found exception:

FATAL EXCEPTION: main
android.content.ActivityNotFoundException: No Activity found to handle Intent {act=edu.example.sharing.action.STORE dat=file:///storage/sdcard0/Download/Alarcon12-Rigoberto.pdf }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
at android.app.Activity.startActivityForResult(Activity.java:3351)
at android.app.Activity.startActivityForResult(Activity.java:3312)

是什么造成的Andr​​oid不承认第二个应用程序申报活动?谢谢你。

What's causing Android to not recognize the declared activity in the second application? Thanks.

推荐答案

在热切,这里是我发现的:

After much looking, here's what I found:

当你使用的内置动作类型和附加数据字​​段或当您使用自定义动作类型没有数据字段,一个意图过滤器没有数据元素就可以了。

When you use a built-in action type and attach a data field or when you use a custom action type with no data field, an intent-filter without a data element is ok.

然而,当你定义一个自定义操作和包含一个数据字段,您必须手动设置附加MIME类型的URI。该 Android文档声称

However, when you define a custom action and include a data field, you must manually set the mime-type for the URI attached. The Android documentation claims that

通常的类型是从数据本身推断。通过设置这个属性,您禁用评估和强制显式类型。

Normally the type is inferred from the data itself. By setting this attribute, you disable that evaluation and force an explicit type.

但事实并非如此。当我把在文件:// URI这在 .TXT 结束,Android的分配一个null MIME类型,所以它不会匹配的任何的意图过滤器,甚至可以具有一个数据 * / * MIME类型。我需要手动设置使用意图的类型 setDataAndType()

But that's not the case. When I put in a file:// URI which ended in .txt, Android assigned it a null mime-type, so it wouldn't match any intent-filter, even one with a data and */* mime-type. I needed to manually set the intent's type using setDataAndType().

总之:您的必须的手动定义意图的 MIME类型使用带有数据的自定义操作时

In short: You must manually define an intent's mime-type when using a custom action with data.