4

我正在尝试将意图从应用 A 发送到应用 B。在应用 AI 的活动中,请执行以下操作:

Intent i = new Intent();
i.setAction("com.example.test2.REQUEST_RESPONSE");
i.putExtra("info", "bla bla bla");
startActivityForResult(i, 0);

在应用程序 BI 中,清单中有以下活动:

    <activity android:name=".Receiving"
        android:label="@string/app_name"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.example.test2.REQUEST_RESPONSE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

这在活动的 onCreate 中:

Intent intent = getIntent();
if (intent.getAction().equals("com.example.test2.REQUEST_RESPONSE")) {
    Log.e("Received", "Intent received thank you!");
}

有了这个我收到以下错误:

01-18 12:30:44.950: E/AndroidRuntime(31200): FATAL EXCEPTION: main
01-18 12:30:44.950: E/AndroidRuntime(31200): java.lang.RuntimeException: Unable to     resume activity {com.example.test/com.example.test.MainActivity}:     java.lang.SecurityException: Permission Denial: starting Intent { act=example.test2.REQUEST_RESPONSE cmp=example.test2/.Receiving (has extras) } from ProcessRecord{41ae02d8 31200:com.example.test/u0a10107} (pid=31200, uid=10107) not exported from uid 10115
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.os.Looper.loop(Looper.java:137)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at java.lang.reflect.Method.invokeNative(Native Method)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at java.lang.reflect.Method.invoke(Method.java:511)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at dalvik.system.NativeStart.main(Native Method)
01-18 12:30:44.950: E/AndroidRuntime(31200): Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=com.example.test2.REQUEST_RESPONSE cmp=com.example.test2/.Receiving (has extras) } from ProcessRecord{41ae02d8 31200:com.example.test/u0a10107} (pid=31200, uid=10107) not exported from uid 10115
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.os.Parcel.readException(Parcel.java:1425)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.os.Parcel.readException(Parcel.java:1379)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1870)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1412)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.Activity.startActivityForResult(Activity.java:3370)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.Activity.startActivityForResult(Activity.java:3331)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at com.example.test.MainActivity.onResume(MainActivity.java:125)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.Activity.performResume(Activity.java:5182)
01-18 12:30:44.950: E/AndroidRuntime(31200):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
01-18 12:30:44.950: E/AndroidRuntime(31200):    ... 12 more

如果我从意图过滤器中删除类别,则会收到此错误:

01-18 12:21:03.059: E/AndroidRuntime(30865): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.test2.REQUEST_RESPONSE (has extras) }

有任何想法吗?

4

2 回答 2

13

如下更改活动清单标签,以便它打开第二个应用程序。

<activity android:name=".Receiving"
        android:label="@string/app_name"
        android:exported="true" >
        <intent-filter>
            <action android:name="com.example.test2.REQUEST_RESPONSE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

崩溃的原因是因为您设置android:exported="false"了这意味着其他进程无法访问此活动。因此,您将其设置为 true 并执行相同操作,它不会崩溃。

于 2013-01-18T12:23:36.360 回答
0

您必须显示另一个应用程序的活动。如果您调用另一个应用程序的活动类,它将将该活动类放在前面,但在我的一个项目中,我调用了将在后台工作的 BroadCastReceiver。

这是您在清单文件中注册接收器并在意图过滤器中为该接收器提供 URL SCHEME 的方式

    <receiver android:name="com.example.exampleapp.ExampleReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

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

            <data android:scheme="receiver" />
        </intent-filter>
    </receiver>

现在从应用程序 A 中打开一个 Intent

    Intent mainIntent = new Intent();
    mainIntent = new Intent(Intent.ACTION_VIEW);
    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mainIntent.setData(Uri.parse("receiver://123"));
    context.sendBroadcast(mainIntent);

这个 setData 将数据传递给其他应用程序,例如 put extra do 将数据传递给另一个类。

我希望这可以帮助你 :)

于 2013-11-29T11:20:59.093 回答