使用intent在两个Android应用程序之间传递数据

时间:2017-02-28 14:43:45

标签: java android android-intent

我有两个独立的Android应用程序,AppA和AppB。我正在尝试让AppA推出AppB(这是一款游戏应用)。用户完成游戏(AppB)后,它会将游戏记录发送回AppA。

因此,AppA正在正确启动AppB,但是当用户完成游戏(AppB)时,AppB在将数据发送回AppA时崩溃,并且出现此错误:

  

处理:com.joy.AppB,PID:20265                                                                       android.content.ActivityNotFoundException:无法找到显式活动类{com.joy.AppA / com.joy.AppA.views.activities.StartGameActivity};你有没有在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的主要活动:

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 个答案:

答案 0 :(得分:1)

试试这个:

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>

然后从app b向app app活动发送数据,请执行以下操作:

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

答案 1 :(得分:0)

也许它有用:

内容提供商和内容解析程序组件

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

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

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

希望它有所帮助!