繁体   English   中英

将Android Webview JS接口方法传递为回调函数

[英]Passing Android webview JS interface method as callback function

我有以下android代码:

private class MyJavaInterface {
    @android.webkit.JavascriptInterface
    public void call(String number) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number));

        startActivity(intent);
    }
}

...

mWebView.addJavascriptInterface(new MyJavaInterface(), "MyInterface");

此JS代码可正常运行:

MyInterface.call(number);

并且这不:

var call = MyInterface.call;
call(number);

我究竟做错了什么?

此JS代码可正常运行:

MyInterface.call(number);

是的,这会很好用,因为Android会自动找到该类和唯一带有注释的公共方法。

对于你的情况

MyInterface.call(number) -> new MyJavaInterface().call(number)

但是第二个不起作用,

MyInterface.call -> new MyJavaInterface().call - there is no public variable in your class and also there is no support for variable in addJavascriptInterface

注意: 参考

对于以API级别JELLY_BEAN_MR1及更高版本为目标的应用程序,只能从JavaScript访问用JavascriptInterface注释的公共方法。 对于以API级别JELLY_BEAN或更低的 API为目标的应用程序,可以访问所有公共方法(包括继承的方法)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM