Android : 关于webview的加载及缓存的总结

1.WebView的介绍

webview是Android中直接加载html页面的控件,它为webApp带来了新生命。那么,他的出现也伴随着很多问题的产生;今天就webview的加载及缓存方面的知识做个总结,希望对大家有所帮助。

2.Webview的缓存

webview的缓存分为两种:网页数据缓存和H5缓存
①网页缓存的结构:
/data/data/package_name/cache/
/data/data/package_name/database/webview.db
/data/data/package_name/database/webviewCache.db
②H5缓存结构:
根据setAppCachePath(String appCachePath)提供的路径,在H5使用缓存过程中生成的缓存文件。

1.配置用户权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permissi on android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  1. webSettings的设置:
WebSettings webSettings = myWebView.getSettings();
//设置渲染的优先级
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
// 开启 DOM storage API 功能
webSettings.setDomStorageEnabled(true);
//开启 database storage API 功能
webSettings.setDatabaseEnabled(true);
String cacheDirPath = getFilesDir().getAbsolutePath() + APP_CACAHE_DIRNAME;
//设置数据库缓存路径
webSettings.setDatabasePath(cacheDirPath);
//设置 Application Caches 缓存目录
webSettings.setAppCachePath(cacheDirPath);
//开启 Application Caches 功能
webSettings.setAppCacheEnabled(true);

webSettings.setLoadWithOverviewMode(true);
//设置WebView支持JavaScript
webSettings.setJavaScriptEnabled(true);
//设置可以访问文件
webSettings.setAllowFileAccess(true);
//设置支持缩放
webSettings.setBuiltInZoomControls(true);
webSettings.setDefaultTextEncodingName("UTF-8");

3.判断是否连接网络:

/**
* 检测当前网络可用
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
// 当前网络是连接的
if (info.getState() == NetworkInfo.State.CONNECTED) {
// 当前所连接的网络可用
return true;
}
}
}
return false;
}

4.针对网络状况采取不同的缓存策略:

if (isNetworkAvailable(getApplicationContext())) {
//有网络连接,设置默认缓存模式
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
} else {
//无网络连接,设置本地缓存模式
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}

在有网络连接的时候,采取默认缓存模式(根据cache-control决定是否从网络上取数据。)

WebSettings.LOAD_DEFAULT

在无网络连接的时候,采取

WebSettings.LOAD_CACHE_ELSE_NETWORK

只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。如:m.taobao.com的cache-control为no-cache,在模式LOAD_DEFAULT下,无论如何都会从网络上取数据,如果没有网络,就会出现错误页面;在LOAD_CACHE_ELSE_NETWORK模式下,无论是否有网络,只要本地有缓存,都使用缓存。本地没有缓存时才从网络上获取。

3.WebView的内存泄漏

首先声明,webview如果不做任何处理的话,会存在内存泄漏的风险;规范的代码,一般对webview都做了防止OOM的处理的。

1.先从布局下手,动态创建WebView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zhangxing.webviewcachedemo.MainActivity">

<LinearLayout
android:id="@+id/webview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>

</RelativeLayout>

在代码中动态创建

webViewLayout = (LinearLayout) findViewById(R.id.webview_layout);
myWebView = new WebView(getApplicationContext());
webViewLayout.addView(myWebView);

2.在OnDestroy()中销毁

@Override
protected void onDestroy() {
super.onDestroy();
myWebView.removeAllViews();
myWebView.destroy();
}

4.关于webview加载的重点回顾
1.设置对JS的支持

//设置WebView支持JavaScript
webSettings.setJavaScriptEnabled(true);

2.设置通过JS打开新窗口的支持:

webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.requestFocus();

3.为webview设置webChromClient,一般主要处理脚本的执行,或者progress的执行

myWebView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(newProgress == 100){
if(dialog != null && dialog.isShowing()){
dialog.dismiss();
}
}else{
if(dialog == null){
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("正在加载中.....");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(newProgress);
dialog.show();
}else{
dialog.setProgress(newProgress);
}
}
}
});

4.为webview设置webviewClient,主要处理关于页面跳转,页面请求等操作

/**
* WebViewClient帮助webView处理一些页面控制和请求通知
* 当点击webview控件中的链接时,在这里设置显示在webview中,否则,会在浏览器打开
*/
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//设置网页在webview控件中打开,false在浏览器中打开
view.loadUrl(url);
return true;
}
});

转载自:https://blog.csdn.net/zhangxing52077/article/details/53730148/

猜你喜欢

转载自blog.csdn.net/qq_36437339/article/details/81017143