我有兩個獨立的應用程序,AppA(使用Android Studio開發)和AppB(使用Unity開發)。 AppA將推出AppB(這是一款遊戲應用)。用戶完成遊戲(AppB)並單擊註銷按鈕後,它會將遊戲記錄(字符串數組)發送回AppA。使用Unity應用程序的意圖發送數據到Android應用程序
目前,AppA能夠啓動AppB,但我無法讓AppB將遊戲記錄(使用意圖)發送回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 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但它gi也有同樣的錯誤。
這是錯誤我得到:
I /統一: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(Native Method)
at com.unity3d.pl ayer.UnityPlayer.a(來源不明)
在com.unity3d.player.UnityPlayer $ C $ 1.handleMessage(來源不明)
在android.os.Handler.dispatchMessage(Handler.java:98)
的機器人。 os.Looper.loop(Looper.java:135)
at com.unity3d.player.UnityPlayer $ c.run(Unknown Source)
引起: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]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
在java.lang.ClassLoader.loadClass(ClassLoader.java:511)
在java.lang.ClassLoader中
煩請幫我!在此先感謝您的幫助! :)
即使我找了相同的答案。我提出了這個問題。 –
作爲一個小小的幫助,AndroidJavaClass構造函數中的className應該是您想要的類,而不是您的應用程序的類。 更改 AndroidJavaClass Intent = new AndroidJavaClass(className); 至 AndroidJavaClass Intent = new AndroidJavaClass(「android.content.Intent」); 這個頁面有一些有用的例子。 你只需要將代碼轉換爲JNI:[link](https://developer.android.com/training/sharing/send.html) – CasualCoder