我有以下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);我做错了什么?
发布于 2017-08-08 21:35:57
此JS代码正确工作: MyInterface.call(数);
是的,这会很好,因为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和更高版本为目标的应用程序,只有使用JavascriptInterface注释的公共方法才能从JavaScript中访问。对于针对API级JELLY_BEAN或低于的应用程序,可以访问所有公共方法(包括继承的方法)。
https://stackoverflow.com/questions/45546281
复制相似问题