FlappySocks
FlappySocks

Reputation: 3880

Monodroid JavaScript Interface

Monodroid does not yet natively support JavaScriptInterface with WebView.

I'm looking for an example .java file that can be used with this workaround.

IntPtr JavaScriptInterface_Class = JNIEnv.FindClass ("the/package/for/JavaScriptInterface");
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");

Upvotes: 4

Views: 619

Answers (2)

l4nk4b3l
l4nk4b3l

Reputation: 11

I know that this thread is already a bit old. But i found this when was looking for the same and here is the solution how you can use c# methods

public class AndroidInterface : Java.Lang.Object
{
        [Export]
        public void Save(string text)
        {

        }
}

AndroidInterface androidInterace = new AndroidInterface();
webView.AddJavascriptInterface(androidInterface, "Android");

Upvotes: 1

jonp
jonp

Reputation: 13600

You could use a custom .java such as:

// TODO: use an actually valid package name. :-)
package the.package.for;

public class JavaScriptInterface {
    // The JNI in the original question uses a default constructor.
    // Either provide one explicitly or use the implicit one...
    public JavaScriptInterface ()
    {
    }

    // TODO: add any methods you want invokable from JavaScript here.
}

Don't forget to set the Build action for your .java file to to AndroidJavaSource.

Upvotes: 2

Related Questions