使用 Intent 从 Unity 应用程序向 Android 应用程序发送数据

我有两个独立的应用程序,AppA(使用 Android Studio 开发)和 AppB(使用 Unity 开发).AppA 将启动 AppB(这是一个游戏应用程序).用户玩完游戏(AppB)并点击退出按钮后,它会将游戏记录(字符串数组)发送回AppA.

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.

目前,AppA 可以启动 AppB,但我无法让 AppB 将游戏记录(使用 Intent)发送回 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 的 StartGameActivity :

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 的 Android 清单:

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 的 C# 代码:

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;
}

我尝试将 "com.joy.AppA.views.activities.StartGameActivity" 放在 Unity 中的 className 中,但它也给出了同样的错误.

I tried putting "com.joy.AppA.views.activities.StartGameActivity" for the className in Unity but it gives the same error too.

这是我得到的错误:

I/Unity: AndroidJavaException: java.lang.ClassNotFoundException: com.joy.AppAjava.lang.ClassNotFoundException: com.joy.AppA
在 java.lang.Class.classForName(Native Method)
在 java.lang.Class.forName(Class.java:309)
在 java.lang.Class.forName(Class.java:273)
在 com.unity3d.player.UnityPlayer.nativeRender(本机方法)
在 com.unity3d.player.UnityPlayer.a(来源不明)
在 com.unity3d.player.UnityPlayer$c$1.handleMessage(来源不明)
在 android.os.Handler.dispatchMessage(Handler.java:98)
在 android.os.Looper.loop(Looper.java:135)
在 com.unity3d.player.UnityPlayer$c.run(来源不明)
引起:java.lang.ClassNotFoundException:在路径上找不到类com.joy.AppA":
DexPathList[[zip 文件/data/app/com.joy.AppB-1/base.apk"],nativeLibraryDirectories=[/data/app/com.joy.AppB-1/lib/arm,/v​​endor/lib,/system/lib]]
在 dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
在 java.lang.ClassLoader.loadClass(ClassLoader.java:511)
在 java.lang.ClassLoader

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! :)



 1 条回答