【发布时间】:2019-02-01 16:27:00
【问题描述】:
这是我使用的代码..
webview.getSettings().setJavaScriptEnabled(true); // enable javascript
webview.addJavascriptInterface(new
WebViewJavaScriptInterface(this), "Android");
和Javascript接口类是
公共类 WebViewJavaScriptInterface{
private Context context;
WebView webView;
/*
* Need a reference to the context in order to sent a post message
*/
public WebViewJavaScriptInterface(Context context){
this.context = context;
Log.d("Inside Interface","Hello Vinod Dirishala");
}
/*
* This method can be called from Android. @JavascriptInterface
* required after SDK version 17.
*/
@JavascriptInterface
public void sendDataToDevice(String usertype,String userid){
Log.d("Inside SendData2Device","Hello Vinod Dirishala");
}
}
上面的 WebViewInterface 构造函数正在调用,但 javascript 方法 sendDataToDevice 方法正在调用,这是在 php 脚本中定义的 javascript 方法,如
Android.sendDataToDevice(val1,val2);
【问题讨论】:
标签: android methods webview constructor