在两个安卓应用程序之间使用意图传递数据

6
我有两个不同的安卓应用,AppA和AppB。我正在尝试让AppA启动一个游戏应用(即AppB)。当用户完成玩游戏(AppB)后,它将向AppA发送游戏记录。
所以,AppA成功启动了AppB,但是当用户完成游戏(AppB)并且尝试将数据返回给AppA时,AppB崩溃了,并出现了以下错误信息:
Process: com.joy.AppB, PID: 20265 android.content.ActivityNotFoundException: Unable to find explicit activity class {com.joy.AppA/com.joy.AppA.views.activities.StartGameActivity}; have you declared this activity in your AndroidManifest.xml?
AppA包名:com.joy.AppA 活动类名称:com.joy.AppA.views.activities.StartGameActivity
AppB包名:com.joy.AppB 活动类名称:com.joy.AppB.MainActivity
我已经做了以下工作:
AppA的StartGameActivity:
//To launch AppB game
Intent launchGameIntent = getPackageManager().getLaunchIntentForPackage("com.joy.AppB");
startActivity(launchGameIntent);

//To retrieve game scores from AppB game
Intent intent = getIntent();
String[] gameRecords_array = intent.getStringArrayExtra("gameRecord");

AppA的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joy.AppA">
.
.
.
<activity
        android:name="com.joy.AppA.views.activities.StartGameActivity"
        android:label="Start Game">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".views.activities.DashboardActivity" />
    </activity>

AppB的MainActivity:

Intent i = new Intent();
i.setComponent(new ComponentName("com.joy.AppA","com.joy.AppA.views.activities.StartGameActivity"));
i.setAction(Intent.ACTION_SEND);
i.putExtra("gameRecord", gameRecord_array);
startActivity(i);

AppB的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joy.AppB" >

<supports-screens android:resizeable="true" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
.
.
.

感谢您提前的帮助!

2个回答

3

请尝试以下方法:

AppA的AndroidManifest.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.joy.AppA">
    .
    .
    .
    <activity
            android:name="com.joy.AppA.views.activities.StartGameActivity"
            android:label="Start Game">

            <intent-filter>
                <action android:name="com.joy.AppA.views.activities.LAUNCH_IT" />
            <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".views.activities.DashboardActivity" />

        </activity>

要从应用程序B向应用程序A的活动发送数据,请按以下步骤操作:
Intent i = new Intent();
i.setAction("com.joy.AppA.views.activities.LAUNCH_IT");
i.putExtra("gameRecord", gameRecord_array);
startActivity(i);

谢谢您的帮助!我想问一下,您是否知道如何让AppA从AppB接收多个字符串数组?目前,AppB一次发送多个字符串数组到AppA,但是AppA只能接收到第一个字符串数组。谢谢! - unintendedjoy
@unintendedjoy,很高兴我的回答对您有帮助。如果我正确理解了您的评论问题,您想要将不同的数组发送到活动中?您可以这样做:Intent i = new Intent(); i.setAction("com.joy.AppA.views.activities.LAUNCH_IT"); i.putExtra("gameRecordArray1", gameRecord_array); i.putExtra("gameRecordArray2", gameRecord_array); i.putExtra("gameRecordArray3", gameRecord_array); i.putExtra("gameRecordArray4", gameRecord_array); startActivity(i); - Elvis Chidera
实际上,我的意思是这样的,你可以从我发布的这个问题中看到:通过意图在两个应用程序之间传递多个字符串数组 谢谢! - unintendedjoy

0

或许这会有用:

内容提供者和内容解析器组件

内容解析器向内容提供者发出请求,提供者做出响应。这类似于两个不同应用程序之间的通信。

例如,客户端(解析器)和内容管理器(提供者)。

这是一个示例教程:https://www.tutorialspoint.com/android/android_content_providers.htm

希望对您有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接