Monodroid JavaScript Interface

≡放荡痞女 提交于 2019-12-21 19:27:13

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/7780677/monodroid-javascript-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!