十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本文介绍了Android中js和原生交互的示例代码,分享给大家,具体如下:

加载webview的类
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JavaScriptInterface JSInterface;
WebView wv;
wv = (WebView) findViewById(R.id.wv_test);
wv.getSettings().setJavaScriptEnabled(true); ///------- 设置javascript 可用
JSInterface = new JavaScriptInterface(this); ////------
wv.addJavascriptInterface(JSInterface, "JSInterface"); // 设置js接口 第一个参数事件接口实例,第二个是实例在js中的别名,这个在js中会用到
wv.loadUrl("file:///android_asset/test.html");
}
}