我正在研究在Android上运行的Unity应用程序(我们称之为App A)。我必须将一些代码从另一个Android Unity应用程序(App B)移到我的Unity应用程序中。
我需要移动到App A的代码负责查询IMU(内部测量单元)。这是App B中存在的相关代码
AndroidJavaObject ActivityObject;
AndroidJavaClass ActivityClass;
AndroidJNI.AttachCurrentThread();
ActivityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
ActivityObject = ActivityClass.GetStatic<AndroidJavaObject>("currentActivity");
tagID = ActivityObject.Call<string>("getTAGID");
以下是App B的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.indotraq.rtls" android:versionName="1.0.0" android:versionCode="1">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:debuggable="false">
<meta-data android:name="com.samsung.android.vr.application.mode" android:value="vr_only"/>
<activity
android:name="com.indotraq.android.rtls.MainActivity"
android:screenOrientation="landscape"
android:launchMode="singleTask"
android:configChanges="screenSize|orientation|keyboardHidden|keyboard"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
</activity>
</application>
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" />
<uses-feature android:glEsVersion="0x00020000" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
</manifest>
这是App A的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sensics.palace" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<application android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
</application>
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="25" />
<uses-feature android:glEsVersion="0x00020000" />
<supports-gl-texture android:name="GL_AMD_compressed_ATC_texture" />
<supports-gl-texture android:name="GL_ATI_texture_compression_atitc" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
我需要将App B清单的相关部分复制到App A的清单中,这就是我遇到的问题。
所以在App A中,有一个辅助线程。该线程存在,以便我们的应用程序可以在将应用程序放入后台时进行一些处理。这个帖子已经存在了很长时间,当App A被放入后台时,尽职尽责地继续执行。
我确实尝试过各种方法将清单文件合并在一起。我尝试的第一件事是将活动合并为一个。我发现除非活动被命名为&#34; com.unity3d.player.UnityPlayerNativeActivity&#34;,否则当应用程序放入后台时,我的后台线程将无法运行。
这是我最近一次尝试的表现。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.indotraq.rtls" android:versionName="1.0.0" android:versionCode="1">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:debuggable="false">
<meta-data android:name="com.samsung.android.vr.application.mode" android:value="vr_only"/>
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
</activity>
<activity
android:name="com.indotraq.android.rtls.MainActivity"
android:screenOrientation="landscape"
android:launchMode="singleTask"
android:configChanges="screenSize|orientation|keyboardHidden|keyboard"
android:label="@string/app_name" >
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>
</application>
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" />
<uses-feature android:glEsVersion="0x00020000" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
</manifest>
使用此清单,我的线程在后台运行得很好,但是这段代码
LogManager.Log("At pos 1");
AndroidJNI.AttachCurrentThread();
LogManager.Log("At pos 2");
ActivityClass = new AndroidJavaClass("com.indotraq.android.rtls.MainActivity");
if(ActivityClass == null) {
LogManager.Log("ActivityClass == null");
}
else {
LogManager.Log("ActivityClass != null");
}
LogManager.Log("At pos 3");
ActivityObject = ActivityClass.GetStatic<AndroidJavaObject>("currentActivity");
LogManager.Log("At pos 4");
tagID = ActivityObject.Call<string>("getTAGID");
LogManager.Log("At pos 5");
生成此输出。
at pos 1 At pos 2 ActivityClass!= null at pos 3
AndroidJavaException:java.lang.NoSuchFieldError:no &#34; Ljava /郎/对象;&#34; field&#34; currentActivity&#34;在班上 &#34; LCOM / indotraq /机器人/ RTLS / MainActivity;&#34;或其超级班 java.lang.NoSuchFieldError:no&#34; Ljava / lang / Object;&#34;领域 &#34; currentActivity&#34;在课堂&#34; Lcom / indotraq / android / rtls / MainActivity;&#34; 或者它的超类 在com.unity3d.player.UnityPlayer.nativeRender(原生方法) 在com.unity3d.player.UnityPlayer.c(未知来源) 在com.unity3d.player.UnityPlayer $ c $ 1.handleMessage(未知来源) 在android.os.Handler.dispatchMessage(Handler.java:98) 在android.os.Looper.loop(Looper.java:145) 在com.unity3d.player.UnityPlayer $ c.run(未知来源) 在UnityEngine.AndroidJNISafe.CheckException()[0x00000]中:0 在UnityEngine.AndroidJNISafe.GetStaticFieldID(IntPtr clazz,System.String name,System.String sig)[0x00000] in:0,UnityEngine._AndroidJNIHelper.GetFieldID(IntPtr) jclass,System.String fieldName,System.String签名,布尔值 isStatic)[0x00000]
对于我需要做些什么来深入了解这项工作将非常感激。
谢谢 约翰劳里
答案 0 :(得分:0)
正如错误所述,com.indotraq.android.rtls.MainActivity
类中没有“ currentActivity ”字段。 currentActivity
是com.unity3d.player.UnityPlayer
的成员。您可以在类中创建静态字段,并在 MainActivity 的构造函数中自行分配。像这样的东西会做
public class MainActivity extends AppCompatActivity
{
static public MainActivity currentActivity;
MainActivity()
{
currentActivity = this;
}
}
然后您可以成功调用 非静态 方法tagID = ActivityObject.Call<string>("getTAGID");
PS :它实际上并没有获得“活动”,只是该类的实例,因此,currentInstance
会更合适。