[mono-android] WebView Javascript Interface and exposing methods through JNI
Tomasz Cielecki
tomasz at ostebaronen.dk
Tue Jan 10 04:38:29 EST 2012
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
More information about the Monodroid
mailing list