加载缓存脱机时,在Android的web视图视图、缓存、加载、Android

2023-09-12 07:12:10 作者:莪的心,谢绝访问!

我有它加载的URL从网站的应用程序。现在,我想,该应用程序在离线时使用缓存。但我只是得到了失败的页面,该页面表示,即时通讯没有连接到该网站。起初我设置CacheMode参数为Load_Normal但这并没有帮助。接下来,我尝试了真的傻使用ConnectivityManager方式:

I have an application which loads urls from a website. Now I want that the application to use the cache when offline. But I just get the failure page which says that im not connected to the website. At first I set the Cachemode to Load_Normal but this doesn't help. Next I tried a realy "silly" approach using the ConnectivityManager:

cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
if(cm.getActiveNetworkInfo().isConnected()){
  mfnWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
  mfnWebView.loadUrl(url);
}
else{
  mfnWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  mfnWebView.loadUrl(url);
}

但这只是导致崩溃的应用程序。

but this just leads to crashing the application.

有没有一种简单的方式来加载时,离线和现有的,只是不存在则显示失败消息的缓存。

Is there a simple way to load the cache when offline and existing and just if not existing showing the failure message.

推荐答案

确定。在code以上的罚款。需要被添加的许可是:

OK. The code is fine above. The permission needed to be added are:

。互联网

.ACCESS_NETWORK_STATE

.ACCESS_NETWORK_STATE

.ACCESS_WIFI_STATE

.ACCESS_WIFI_STATE