保存android WebView缓存并加载无互联网

时间:2014-12-28 17:25:37

标签: android caching webview load

我搜索并没有找到任何可以帮助我解答的问题。

我的WebView代码:

webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());
WebView webView = (WebView) findViewById(R.id.webView1);
initWebView(webView);

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setAppCacheMaxSize(1024*1024*8);   
webView.getSettings().setAppCachePath("/cache/");  
webView.getSettings().setAppCacheEnabled(true);

webView.loadUrl("http://google.com");
webView.setWebViewClient(new WebViewClient());

1 个答案:

答案 0 :(得分:2)

使用此

ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
if(connectivityManager.getActiveNetworkInfo().isConnected()){
  mfnWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
  mfnWebView.loadUrl(url);
}
else{
  mfnWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  mfnWebView.loadUrl(url);
}

强制权限: - INTERNET,ACCESS_NETWORK_STATE,ACCESS_WIFI_STATE