- Home /
Sending data using intent from Unity app to Android app
I have two separate apps, AppA (developed using Android Studio) and AppB (developed using Unity). AppA will launch AppB (which is a game app). After the user is done playing the game (AppB) and click the logout button, it will send the game records (string arrays) back to AppA.
Currently, AppA is able to launch AppB, but I can't get AppB to send the game records (using intent) back to AppA.
AppA's StartGameActivity :
Intent launchGameIntent = getPackageManager().getLaunchIntentForPackage("com.joy.AppB");
startActivity(launchGameIntent);
retrieveGameRecords = (Button) findViewById(R.id.retrieveRecordsButton);
retrieveGameRecords.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Retrieving game records from game app
database = new gameDbHelper(StartGameActivity.this.getApplicationContext()).getWritableDatabase();
Intent intent = getIntent();
String[] gameRecords_array = intent.getStringArrayExtra("gameRecord");
System.out.println("Number of game records received from game : " + gameRecords_array.length);
AppA's Android Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joy.AppA">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.joy.AppA.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.joy.AppA.permission.C2D_MESSAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:largeHeap="true"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="android.support.multidex.MultiDexApplication">
<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>
AppB's C# code:
public void sendDataToAppAFunction()
{
int counter = 0;
string className = "com.joy.AppA";
string[] gameRecord_strArray = new string[100];
AndroidJavaObject launchIntent = null;
AndroidJavaClass Intent = new AndroidJavaClass (className);
AndroidJavaObject sendIntent = new AndroidJavaObject (className);
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
sendIntent.Set<AndroidJavaObject> ("gameRecord", javaArrayFromCS(gameRecord_strArray));
sendIntent.Call<AndroidJavaObject> ("putExtra", Intent.GetStatic<string[]>("gameRecord"), gameRecord_strArray);
currentActivity.Call ("startActivity", sendIntent);
}
private AndroidJavaObject javaArrayFromCS(string [] values) {
AndroidJavaClass arrayClass = new AndroidJavaClass ("java.lang.reflect.Array");
AndroidJavaObject arrayObject = arrayClass.CallStatic<AndroidJavaObject> ("newInstance", new AndroidJavaClass ("java.lang.String"), values.Length);
for (int i = 0; i < values.Length; ++i) {
arrayClass.CallStatic ("set", arrayObject, i, new AndroidJavaObject ("java.lang.String", values [i]));
}
return arrayObject;
}
I tried putting "com.joy.AppA.views.activities.StartGameActivity" for the className in Unity but it gives the same error too.
This is the error I get:
I/Unity: AndroidJavaException: java.lang.ClassNotFoundException: com.joy.AppA java.lang.ClassNotFoundException: com.joy.AppA at java.lang.Class.classForName(Native Method) at java.lang.Class.forName(Class.java:309) at java.lang.Class.forName(Class.java:273) at com.unity3d.player.UnityPlayer.nativeRender(Native Method) at com.unity3d.player.UnityPlayer.a(Unknown Source) at com.unity3d.player.UnityPlayer$c$1.handleMessage(Unknown Source) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:135) at com.unity3d.player.UnityPlayer$c.run(Unknown Source) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.joy.AppA" on path: DexPathList[[zip file "/data/app/com.joy.AppB-1/base.apk"],nativeLibraryDirectories=[/data/app/com.joy.AppB-1/lib/arm, /vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader
Please kindly help me! Thanks in advance for your help! :)