[mono-android] WebView Javascript Interface and exposing methods through JNI
Tomasz Cielecki
tomasz at ostebaronen.dk
Tue Jan 10 04:58:54 EST 2012
My guess it has something to do with my implementation of my
GetNotifyHandler. It looks like this:
static Delegate cb_notify;
static Delegate GetNotifyHandler()
{
if (cb_notify == null)
cb_notify =
JNINativeWrapper.CreateDelegate((Action<IntPtr, IntPtr,
String>)n_Notify);
return cb_notify;
}
static void n_Notify(IntPtr jnienv, IntPtr lrefThis, String a)
{
ManagedAccessControlJavascriptNotify __this =
Java.Lang.Object.GetObject<ManagedAccessControlJavascriptNotify>(lrefThis,
JniHandleOwnership.DoNotTransfer);
__this.Notify(a);
}
I have also tried putting Java.Lang.String as parameter in that
Action, but that results in an Unhandled exception with the message:
The type `Java.Lang.String' layout needs to be Sequential or Explicit
My question is. 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. Is it able to cast it or is this why I get null as
parameter passed into my method?
On Tue, Jan 10, 2012 at 10:38 AM, Tomasz Cielecki <tomasz at ostebaronen.dk> wrote:
> OK, So it kind of works, but there is a small problem. The notify
> method I override always has null as its parameter when a result of
> not being null is expected. So the Javascript Interface invokes the
> method, but nothing is passed along as parameter.
>
> So investigating a bit I tried just instantiating the Java object and
> pass it directly like this:
>
> IntPtr AccessControlJavascriptNotify_Class =
> JNIEnv.FindClass("monodroid/watoolkit/library/login/AccessControlJavascriptNotify");
> IntPtr AccessControlJavascriptNotify_Ctor =
> JNIEnv.GetMethodID(AccessControlJavascriptNotify_Class, "<init>",
> "()V");
> IntPtr AccessControlJavascriptNotify_Instance =
> JNIEnv.NewObject(AccessControlJavascriptNotify_Class,
> AccessControlJavascriptNotify_Ctor);
>
> webView.AddJavascriptInterface(new
> Java.Lang.Object(AccessControlJavascriptNotify_Instance,
> JniHandleOwnership.TransferLocalRef), "external");
>
> I edited my Java class to be a bit verbose:
>
> package monodroid.watoolkit.library.login;
> import android.util.Log;
>
> public class AccessControlJavascriptNotify
> {
> private static final String TAG = "AccessControlJavascriptNotify";
> public void notify(String securityTokenResponse) {
> Log.v(TAG, securityTokenResponse); }
> }
>
> This spits out what I want in the Log unlike the managed code. Any
> reason to why there is a difference?
>
> What I want to do with the result is save it and fire an event when I
> get it, so I need a way to do this.
>
> On Tue, Jan 10, 2012 at 3:46 AM, Jonathan Pryor <jonp at xamarin.com> wrote:
>> On Jan 9, 2012, at 6:42 PM, Tomasz Cielecki wrote:
>>> Would it be enough just to copy it into the main project?
>>
>> Yes, or Link the file into your main project. The key point is that if you want it in the .apk and it's not C# code, it needs to be in the main project. This includes resources, Java source, Java libraries, assets...
>>
>> - 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