大致代码是这样的
相信大家都能搜到完整代码,这里只贴大致意思
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JavaScriptInterface(
MainActivity.this), "android");
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
// 如果target 大于等于API 17,则需要加上如下注解
@JavascriptInterface
public void localMethod() {
// Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
Toast.makeText(mContext, "success", Toast.LENGTH_LONG).show();
}
}
不过试了好久都没反应
需要注意一下
箭头指向的代码不要省略(有时候IDE不自动生成,不报错)