Passing data between two android apps using intent

2019-03-01 00:41发布

问题:

I have two separate android apps, AppA and AppB. I am trying to get AppA to launch AppB (which is a game app). After the user is done playing the game (AppB), it will send the game records back to AppA.

So, AppA is launching AppB correctly, but when user is done with playing the game (AppB), AppB crashes while sending the data back to AppA, and this error shows up:

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 package name : com.joy.AppA
Activity class name : com.joy.AppA.views.activities.StartGameActivity

AppB package name : com.joy.AppB
Activity class name : com.joy.AppB.MainActivity


Here is what I've done so far:

AppA's 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's 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's 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's 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>
.
.
.

Thanks in advance for your help!

回答1:

Try this:

AppA's 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>

Then to send data to app a activity from app b, do this:

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


回答2:

Maybe it can be useful:

Content Provider and Content Resolver components.

Content Resolver makes request to Content Provider, and the provider respond. This is similar to a communication between two different applications.

For example a client (Resolver) and a content manager(Provider).

This is an example tutorial: https://www.tutorialspoint.com/android/android_content_providers.htm

Hope it helps!