[mono-android] WebView Javascript Interface and exposing methods through JNI

Jonathan Pryor jonp at xamarin.com
Tue Jan 10 11:08:28 EST 2012


One of these days we'll get the JNI doc published which discusses many of these issues.

In the meantime...

On Jan 10, 2012, at 4:58 AM, Tomasz Cielecki wrote:
> My guess it has something to do with my implementation of my GetNotifyHandler.

Not GetNotifyHandler(), but n_Notify():

>        static void n_Notify(IntPtr jnienv, IntPtr lrefThis, String a)

What's going on is a "reverse P/Invoke": you're creating a delegate (`cb_notify`), which is passed to native code (Dalvik), and the delegate is invoked. This means that you need to stick to types that the P/Invoke marshaler knows about.

In this case, the P/Invoke marshaler will see a `string` parameter, assume that it's a UTF-8 `const char*` value, and behaves accordingly. However, that's not what the parameter will be, it'll be a java.lang.String instance, which the P/Invoke marshaler knows nothing about.

The solution is to manually marshal things:

	static void n_Notify(IntPtr jnienv, IntPtr lrefThis, IntPtr native_a)
	{
		ManagedAccessControlJavascriptNotify __this = Java.Lang.Object.GetObject<ManagedAccessControlJavascriptNotify>(
				lrefThis, JniHandleOwnership.DoNotTransfer);
		Java.Lang.String a = Java.Lang.Object.GetObject<Java.Lang.String>(
				a, JniHandleOwnership.DoNotTransfer);
		__this.Notify (a);
	}

> What happens if the Javascript interface wants to pass a Java.Lang.String to the managed code and the Action there has the type System.String.

Convert the Java.Lang.String to a System.String by using Java.Lang.String.ToString(). :-)

 - Jon



More information about the Monodroid mailing list