[mono-android] WebView Javascript Interface and exposing methods through JNI
Tomasz Cielecki
tomasz at ostebaronen.dk
Wed Jan 11 07:00:17 EST 2012
Just to update, the code works :) Thanks again!
On Tue, Jan 10, 2012 at 6:06 PM, Tomasz Cielecki <tomasz at ostebaronen.dk> wrote:
> Oooooh nice! I will try this out. I really appreciate the help and
> time you put into this!
>
> On Tue, Jan 10, 2012 at 5:08 PM, Jonathan Pryor <jonp at xamarin.com> wrote:
>> 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
>>
>> _______________________________________________
>> Monodroid mailing list
>> Monodroid at lists.ximian.com
>>
>> UNSUBSCRIBE INFORMATION:
>> http://lists.ximian.com/mailman/listinfo/monodroid
>
>
>
> --
> Med Venlig Hilsen / With Best Regards
> Tomasz Cielecki
> http://ostebaronen.dk
--
Med Venlig Hilsen / With Best Regards
Tomasz Cielecki
http://ostebaronen.dk
More information about the Monodroid
mailing list