1、初始化WebView
X5WebView webView =new X5WebView(this,null);我这里用的是腾讯的TBS浏览服务.详情参考上一篇文章:https://www.jianshu.com/writer#/notebooks/10278901/notes/42868406
2、设置初始化WebSetting
WebSettings settings =webView.getSettings();
settings.setJavaScriptEnabled(true);//支持js
settings.setBlockNetworkImage(false);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);//开启本地DOM存储
settings.setJavaScriptCanOpenWindowsAutomatically(true);//支持通过JS打开新窗口
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
settings.setAllowFileAccess(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
3、给WebView添加addJavascriptInterface()
webView.addJavascriptInterface(new JavaInterface(),"aa");//js 调用Java代码
注:这个的aa是和前端人员商量的前端调用时用aa.getUserId(),
new JavaInterface() 这个里面是js要调用Android的方法
例如:
网友评论