当可以访问 Internet 时,我需要从 Internet 加载 mWebview。当它不可用时,它需要从缓存中加载它。我有一些东西,但它根本不加载网站
请帮我?
我已经添加了权限:
- android.permission.INTERNET
- android.permission.WRITE_EXTERNAL_STORAGE
- android.permission.ACCESS_NETWORK_STATE
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Layout Webview
mWebview = new WebView(this);
// Broadcast
Toast.makeText(this, "Laden van roosterwijzigingen.",
Toast.LENGTH_SHORT).show();
// Enable JavaScript
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.setHorizontalScrollBarEnabled(false);
mWebview.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
mWebview.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() );
mWebview.getSettings().setAllowFileAccess( true );
mWebview.getSettings().setAppCacheEnabled( true );
mWebview.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default
final Activity activity = this;
// Make WebClient
mWebview.setWebViewClient(new WebViewClient() {
// Trace Errors
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
if ( !isNetworkAvailable() ) { // loading offline
mWebview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}
mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
}