我目前正在尝试为 android 游戏实现通知系统,并且需要打开应用程序的本机设置以帮助用户在之前关闭时重新启用通知。我写了一个小的java类:
package com.example.plugin;
import android.app.Activity;
import android.net.Uri;
import android.content.*;
import android.provider.Settings;
public class NotificationPlugin {
public void openSettings(Activity unityActivity) {
Intent i = new Intent(Settings.ACTION_APPLICATION_SETTINGS,
Uri.parse("package:" + unityActivity.getPackageName()));
unityActivity.startActivity(i);
}
}
我将这些行统一添加到自定义 AndroidManifest.xml 中。
<intent-filter>
<action android:name="android.settings.APPLICATION_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
我正在使用此方法访问 ac# 包装器类中的方法。
public static void OpenNotificationSettings() {
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
pluginClass = new AndroidJavaClass(pluginName);
pluginClass.Call("openSettings", unityActivity);
}
该变量pluginName
是一个常量字符串,包含插件类的完全限定包名称。
使用 logcat 我可以在尝试执行此操作时看到以下异常:
android.content.ActivityNotFoundException: No Activity found to handle Intent
由于我对 android 和统一使用插件都没有实际经验,我不知道如何处理异常以及所有这些层中的问题所在。谁能告诉我如何解决这个问题?
PS:我知道我可以在没有插件的情况下完成此操作,但是插件中还有其他方法,我需要并且我希望将所有与原生 android 相关的代码打包到一个插件中。
最初的问题是你的统一活动类不在包中,但你试图加载它,就好像它在包中一样。用这个替换你的代码:
package com.example.plugin;
import android.app.Activity;
import android.net.Uri;
import android.content.*;
import android.provider.Settings;
public class NotificationPlugin {
public void openSettings(Context context) {
Intent i = new Intent(Settings.ACTION_APPLICATION_SETTINGS,
//Uri.parse("package:" + unityActivity.getPackageName()));
context.startActivity(i);
}
}
和统一部分:
public static void OpenNotificationSettings() {
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
pluginClass = new AndroidJavaClass(pluginName);
pluginClass.CallStatic("openSettings", unityActivity);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句