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

Java 使用intent从Unity应用程序向Android应用程序发送数据,java,c#,android,android-intent,unity3d,Java,C#,Android,Android Intent,Unity3d,我有两个独立的应用程序,AppA(使用Android Studio开发)和AppB(使用Unity开发)。AppA将启动AppB(这是一款游戏应用程序)。用户玩完游戏(AppB)并单击注销按钮后,它会将游戏记录(字符串数组)发送回AppA 目前,AppA可以启动AppB,但我无法让AppB将游戏记录(使用intent)发送回AppA AppA的StartGameActivity: Intent launchGameIntent = getPackageManager().getLaunchInt

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

目前,AppA可以启动AppB,但我无法让AppB将游戏记录(使用intent)发送回AppA

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

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

public void sendDataToAppFunction()
{
int计数器=0;
string className=“com.joy.AppA”;
string[]gameRecord_strArray=新字符串[100];
AndroidJavaObject launchIntent=null;
AndroidJavaClass Intent=新的AndroidJavaClass(类名);
AndroidJavaObject sendIntent=新的AndroidJavaObject(类名);
AndroidJavaClass unityPlayer=新的AndroidJavaClass(“com.unity3d.player.unityPlayer”);
AndroidJavaObject currentActivity=unityPlayer.GetStatic(“currentActivity”);
Set(“gameRecord”,javaArrayFromCS(gameRecord_strArray));
调用(“putExtra”,Intent.GetStatic(“gameRecord”),gameRecord\u strArray);
调用(“startActivity”,sendIntent);
}
私有AndroidJavaObject javaArrayFromCS(字符串[]值){
AndroidJavaClass arrayClass=新的AndroidJavaClass(“java.lang.reflect.Array”);
AndroidJavaObject arrayObject=arrayClass.CallStatic(“newInstance”,新AndroidJavaClass(“java.lang.String”),values.Length);
对于(int i=0;i
我尝试在Unity中为类名添加“com.joy.AppA.views.activities.StartGameActivity”,但它也给出了相同的错误

这是我得到的错误:

I/Unity:AndroidJavaException:java.lang.ClassNotFoundException:com.joy.AppA java.lang.ClassNotFoundException:com.joy.AppA
位于java.lang.Class.classForName(本机方法)
位于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,/vendor/lib,/system/lib]
位于dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
位于java.lang.ClassLoader.loadClass(ClassLoader.java:511)
在java.lang.ClassLoader


请帮帮我!提前感谢您的帮助!:)

替换这行代码

currentActivity.Call ("startActivity", sendIntent);
用这个

AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", sendIntent, "Share Via");
currentActivity.Call("startActivity", jChooser);
AndroidJavaObject jChooser=intentClass.CallStatic(“createChooser”,sendIntent,“Share Via”);
调用(“startActivity”,jChooser);

就连我也在寻找同样的答案。作为一个小帮助,AndroidJavaClass构造函数中的类名应该是您想要的类,而不是应用程序的类。更改AndroidJavaClass Intent=新的AndroidJavaClass(类名);to AndroidJavaClass Intent=新的AndroidJavaClass(“android.content.Intent”);本页有一些有用的例子。您只需将代码转换为JNI:抱歉,我尝试了,但仍然收到相同的错误消息。我不知道如何解决这个问题。我知道我参加这个聚会有点晚了,但他不想有一个选择器弹出窗口,他只是将意图用作从一个应用程序到另一个应用程序的消息。
AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", sendIntent, "Share Via");
currentActivity.Call("startActivity", jChooser);