我在我的应用中实现了webview,我希望在没有网络离线时加载缓存,但这对我不起作用。这是我的代码:
....
pageInfo = (WebView)findViewById(R.id.pageInfo);
pageInfo.getSettings().setJavaScriptEnabled(true);
pageInfo.getSettings().setLoadWithOverviewMode(true);
pageInfo.getSettings().setUseWideViewPort(true);
pageInfo.getSettings().setBuiltInZoomControls(true);
pageInfo.getSettings().setSupportZoom(true);
pageInfo.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
pageInfo.setScrollbarFadingEnabled(true);
pageInfo.getSettings().setLoadsImagesAutomatically(true);
pageInfo.getSettings().setAppCacheMaxSize(1024 * 1024 * 100);
pageInfo.getSettings().setAppCacheEnabled(true);
if ( !isNetworkAvailable() ) { // loading offline
pageInfo.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
Log.w("tag", "offline");
}
try {
pageInfo.loadUrl(Variabel.link_detail_post);
} catch (Exception e) {
e.printStackTrace();
}
....
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
这是我的许可
<!-- permission -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
那怎么解决这个问题?谢谢...对不起我的英语和语法