问题描述
我有一个从网站加载网址的应用程序.现在我希望应用程序在离线时使用缓存.但我只是得到失败页面,上面说我没有连接到网站.起初我将 Cachemode 设置为 Load_Normal 但这没有帮助.接下来我尝试了一个真正的傻"使用 ConnectivityManager 的方法:
I have an application which loads urls from a website. Now I want 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.
推荐答案
好的.上面的代码没问题.需要添加的权限是:
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
这篇关于在 Android Webview 中离线时加载缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!