Jonathan Pryor <jonp@...> writes: > > On Aug 12, 2011, at 8:56 AM, malexandroni wrote: > > I tried the JS interface in Java and it works fine, but I tried to replicate the code in Mono for Android and it > does not work. What am I doing wrong? > > That won't work at this point in time. > > The problem is reflection: in order for the JavaScript to invoke the method on the provided Java object, > Android uses Java reflection in order to see if the method exists, and if it does to then invoke the method. > This works in Java because the JavaScriptInterface type has a `showToast` method. > > This doesn't work in Mono for Android, because `mandroid` only generates Java method declarations for > methods that override base class methods or implement interface methods. Consequently, your C# > JavaScriptInterface.showToast() method isn't visible to the Java world, and things don't work. (To see > what I mean, look at obj\Debug\android\src\**\JavaScriptInterface.java, and see what methods are present.) > > We intent to support this in a future release (timeframe: unspecified), but that doesn't help _now_. > > What will help now is a workaround: write your JavaScriptInterface type in a .java file, include the .java > file in your project with a AndroidJavaSource Build action, and in Activity1.OnCreate(), do: > > IntPtr JavaScriptInterface_Class = JNIEnv.FindClass ("the/package/for/JavaScriptInterface"); > // TODO: Update "the/package/for" as appropriate for your type. > IntPtr JavaScriptInterface_ctor = JNIEnv.GetMethodID (JavaScriptInterface_Class, "<init>", "()V"); > IntPtr instance = JNIEnv.NewObject (JavaScriptInterface_Class, JavaScriptInterface_ctor); > > appView.AddJavascriptInterface (new Java.Lang.Object (instance), "Android"); > > - Jon > > _______________________________________________ > Monodroid mailing list > Monodroid@... > > UNSUBSCRIBE INFORMATION: > http://lists.ximian.com/mailman/listinfo/monodroid > >
Hi, I would be interessted in a quick fix for this. Could you please post a sample Java file? Can I write my javascript interface code in the appropriate .net class as it has to interact with other stuff within my app. So for my understanding, all we need here is a java type for it? Many thanks Maik _______________________________________________ Monodroid mailing list Monodroid@lists.ximian.com UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid