Xamarin.android 在Mono中使用JNI访问Java库
我正在为Android开发一个使用mono的应用程序,一直在努力让推送通知生效,我正在使用城市飞艇 到目前为止,我已经能够调用TakeOff()和EnablePush(),并且我的应用程序正在成功注册,请参阅以下代码:Xamarin.android 在Mono中使用JNI访问Java库,xamarin.android,urbanairship.com,jnienv,Xamarin.android,Urbanairship.com,Jnienv,我正在为Android开发一个使用mono的应用程序,一直在努力让推送通知生效,我正在使用城市飞艇 到目前为止,我已经能够调用TakeOff()和EnablePush(),并且我的应用程序正在成功注册,请参阅以下代码: //_____________________________ // Get the airship config class IntPtr ip_airshipConfigOptions = JNIEnv.FindClass("com/urbanairship/Ai
//_____________________________
// Get the airship config class
IntPtr ip_airshipConfigOptions = JNIEnv.FindClass("com/urbanairship/AirshipConfigOptions");
if (ip_airshipConfigOptions == IntPtr.Zero)
{
throw new InvalidOperationException("Counldn't find java class !");
}
//__________________________________________________
// Get the loadDefaults method from the config class
IntPtr methodId = JNIEnv.GetStaticMethodID(ip_airshipConfigOptions, "loadDefaultOptions", "(Landroid/content/Context;)Lcom/urbanairship/AirshipConfigOptions;");
if (methodId == IntPtr.Zero)
{
throw new InvalidOperationException("Couldn't find java class !");
}
//________________________________________________________________
// Call the loadDefaultOptions method passing in this app instance
var methodPtr = JNIEnv.CallStaticObjectMethod(ip_airshipConfigOptions, methodId, new JValue(this));
//________________________
// Get the UAirship class
IntPtr ip_uairship = JNIEnv.FindClass("com/urbanairship/UAirship");
if (ip_uairship == IntPtr.Zero)
{
throw new InvalidOperationException("Couldn't find java class !");
}
//___________________________________________
// Get takeOff method with configoption param
IntPtr methodId2 = JNIEnv.GetStaticMethodID(ip_uairship, "takeOff", "(Landroid/app/Application;Lcom/urbanairship/AirshipConfigOptions;)V");
//______________________________________________
// Get takeOff method without configoption param
//IntPtr methodId3 = JNIEnv.GetStaticMethodID(ip_uairship, "takeOff", "(Landroid/app/Application;)V");
//___________________________________________________________________________________________
// Call UAirship.takeOff(this, options). Not sure if these parameters are specified correctly
JNIEnv.CallStaticVoidMethod(ip_uairship, methodId2, new JValue(this), new JValue(methodPtr));
//________________________________________
// Enable Push in Urban Airship Pushmanager
IntPtr ip_pushmanager = JNIEnv.FindClass("com/urbanairship/push/PushManager");
IntPtr ip_enablePush = JNIEnv.GetStaticMethodID(ip_pushmanager, "enablePush", "()V");
JNIEnv.CallStaticVoidMethod(ip_pushmanager, ip_enablePush);
我现在需要调用PushManager.shared().setIntentReciever(myClass),但我似乎无法访问shared()类或setIntentReciever方法。我已经尝试了各种组合来尝试访问该方法,但始终得到类/方法未发现异常
//IntPtr ip_setReciver = JNIEnv.GetStaticMethodID(ip_PushManager, "shared().setIntentReceiver", "(Landroid/app/Class)V");
感谢您的帮助!我相信它可以用一行来解决,我想我只是缺少了一些语法
关于解决此问题,请参阅下面通过JNI调用setIntentReceiver的代码:
//__________________________
// Get the PushManager class
IntPtr ip_Test = JNIEnv.FindClass("com/urbanairship/push/PushManager");
//_____________________
// Get the class object
IntPtr ip_class = JNIEnv.GetObjectClass(ip_Test);
//_________________________________
// Get the setIntentReciever method
IntPtr ip_setIntent = JNIEnv.GetMethodID(ip_Test, "setIntentReceiver", "(Ljava/lang/Class;)V");
//_________________________________________________
// Create a new jValue from the IntentRecieverClass
JValue val = new JValue(reciever.Class);
//______________________________________________________________________________________
// Set the intent reciever by calling the setIntentReciever method passing in the jValue
JNIEnv.CallStaticVoidMethod(ip_class, ip_setIntent, val);
仅供参考,来自java的调用如下所示:PushManager.shared().setIntentReceiver(IntentReceiver.class);