final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo active = cm.getActiveNetworkInfo();
if (active != null && active.isConnected()) {
String appCachePath = getApplicationContext().getCacheDir()
.getAbsolutePath();
webview.getSettings().setAppCachePath(appCachePath);
webview.getSettings().setCacheMode(
WebSettings.LOAD_DEFAULT);
webview.getSettings().setAppCacheMaxSize(1024 * 0124 * 8);
webview.loadDataWithBaseURL("", "<html><head><title>Cache Test</title></head><body><p>This is Icon</p><br/><img src='http://google.com.us/images/icons/product/chrome-48.png'></body></html>", "text/html", "UTF-8", "");
} else {
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
webview.loadDataWithBaseURL("", "<html><head><title>Cache Test</title></head><body><p>This is Icon</p><br/><img src='http://google.com.us/images/icons/product/chrome-48.png'></body></html>", "text/html", "UTF-8", ""); }
清单有:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
但是当我离线打开 webview 时,webview 返回“Null”。请帮我!