4

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");

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.