问题描述
我有两个独立的android应用,即AppA和AppB.我正在尝试让AppA启动AppB(这是一个游戏应用程序).用户玩完游戏(AppB)后,它将把游戏记录发送回AppA.
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.
因此,AppA可以正确启动AppB,但是当用户玩完游戏(AppB)后,AppB在将数据发送回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:
AppA套件名称:com.joy.AppA
活动类名称:com.joy.AppA.views.activities.StartGameActivity
AppA package name : com.joy.AppA
Activity class name : com.joy.AppA.views.activities.StartGameActivity
AppB包名称:com.joy.AppB
活动类名称:com.joy.AppB.MainActivity
AppB package name : com.joy.AppB
Activity class name : com.joy.AppB.MainActivity
这是我到目前为止所做的:
Here is what I've done so far:
AppA的StartGameActivity:
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的AndroidManifest.xml:
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的MainActivity:
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的AndroidManifest.xml:
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!
推荐答案
尝试一下:
AppA的AndroidManifest.xml:
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>
然后将来自应用b的活动的数据发送至应用,执行以下操作:
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);
这篇关于使用意图在两个Android应用之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!