我可以在android中缓存某些网址的WebView
内容,例如google.com
。
只能缓存一些网址。在尝试离线显示某些网址内容时,它显示没有连接。
public class Main2Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main2);
WebView browser = (WebView) findViewById(R.id.webView1);
String weblink = url;
if(isNetworkAvailable()) {
Log.d("TRIAL","ONLINE");
browser.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
browser.loadUrl(weblink);
} else {
Log.d("TRIAL","OFFLINE");
browser.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
browser.loadUrl(weblink);
}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
}