MainWeb.class:
webb = (WebView) findViewById(R.id.nonMain);
webb.setWebViewClient(new WebViewClient());
webb.getSettings().setLoadWithOverviewMode(true);
webb.getSettings().setUseWideViewPort(true);
webb.getSettings().setJavaScriptEnabled(true);
webb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webb.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
webb.getSettings().setAllowFileAccess(true);
webb.getSettings().setAppCacheEnabled(true);
webb.getSettings().setJavaScriptEnabled(true);
webTimer = new Timer();
webTimer.schedule(new TimerTask() {
@Override
public void run() {
reloadSite();
}
}, 0, 1000*60*2); // once 2 hours
reloadSite():
public void reloadSite() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isNetworkAvailable()) { // loading offline
webb.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
System.out.println("OFFLINE");
} else {
webb.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // load online by default
System.out.println("ONLINE");
}
webb.loadUrl("http://screens.company/manage/showadv.php?bid=" + ID);
System.out.println("WEBSITE RELOADED.");
}
});
}
我每两分钟重新加载一次网站,我希望如果我没有互联网连接,它会显示网站的缓存,这样即使没有网络连接也不会更新,它会继续运行。但是,如果我没有网络连接,我会发现"无法找到网站CONNECTION REFUSED"。
编辑:
isNetworksAvailable():
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}