1,xml布局
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2,跳转外部浏览器显示网页
wv_shop.loadUrl(url);
3,本地webview显示网页
wv_shop.setWebViewClient(new WebViewClient() {@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
4,本地webview显示网页的相关属性设置
WebSettings ws=wv_shop.getSettings();ws.setUserAgentString("Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10");
ws.setJavaScriptEnabled(true);//允许js交互
ws.setSupportZoom(true);//支持缩放
ws.setBuiltInZoomControls(true);//触摸屏幕时,显示缩放按钮
ws.setUseWideViewPort(true);// 设置webview推荐使用的窗口,可任意比例缩放
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//自适应页面大小 不能左右滑动
ws.setLoadWithOverviewMode(true);//设置webview加载的页面的模式
ws.setDomStorageEnabled(true);//允许使用DOM存储
ws.setNeedInitialFocus(true);//阻止webview内部获取焦点
ws.setSupportMultipleWindows(true);//是否启用新窗口
ws.setDatabaseEnabled(true);//启用数据库
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);//加载站点资源的行为,设为全部加载,
}
wv_shop.requestFocusFromTouch();
wv_shop.loadUrl(url);
5,js交互
本地调用js方法:show()
wv_shop.loadUrl("javascript:show()")
js调用本地方法:show()
wv_shop.addJavascriptInterface(new JSInterface(),"client");向webview注入js接口对象
class JSInterface{
@JavascriptInterface
public void show(){
}
6,启用webview定位
//启用数据库
ws.setDatabaseEnabled(true);
String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
//启用地理定位
ws.setGeolocationEnabled(true);
//设置定位的数据库路径
ws.setGeolocationDatabasePath(dir);
ws.setDomStorageEnabled(true);
wv.setWebChromeClient(new WebChromeClient(){
/**
* 重写方法,开启定位
* */
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin,true,false);
super.onGeolocationPermissionsShowPrompt(origin, callback);
}
}
7,webview混淆
-keepclassmembers class com.shilian.yibo.utils.JSInterface {
public *;
}
-keepclassmembers class * extends android.webkit.WebChromeClient {
public void openFileChooser(...);
public void onShowFileChooser(...);
}
-keepattributes *Annotation*
-keepattributes *JavascriptInterface*