web视图显示在android的缓存版本 [英] displaying cached version of webview in android

查看:163
本文介绍了web视图显示在android的缓存版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个网站的HTML5离线缓存的版本显示,当网络关闭的WebView内。

我已经覆盖 onReceivedError 确定,而当网络出现故障这一方法被调用。 问题是,它显示了通用的网页不可用的消息。

我怎样才能得到它来显示网页的HTML5缓存版本? web应用程序的离线存储是肯定的工作,因为它工作正常在Firefox和Chrome桌面版。

我知道我可以叫 loadData 到视图手动 onReceivedError ,但林不知道在哪儿能买到HTML5的缓存值的。

请注意:如果我在设置一些虚拟数据 loadData view.loadData(Uri.en code(< HTML&GT ;< D​​IV>页面加载失败< / DIV>< / HTML>中),text / html的,UTF-8); 键,然后单击后退(通过检测回事件调用 webview.goBack(); 那么页面的缓存版本的显示确定

下面是我添加设置web视图的code一些行:

  webview.getSettings()setCacheMode(WebSettings.LOAD_DEFAULT)。
webview.getSettings()setAppCacheMaxSize(1024 * 1024 * 8)。
webview.getSettings()setAppCachePath(/数据/数据​​/ com.stuff.android /缓存)。
。webview.getSettings()setAllowFileAccess(真正的);
webview.getSettings()setAppCacheEnabled(真)。
webview.getSettings()setDomStorageEnabled(真)。
webview.getSettings()setJavaScriptEnabled(真)。
 

解决方案

尝试使用摸清网络状态

 私人布尔isNetworkAvailable(){
    ConnectivityManager connectivityManager
          =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    的NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    返回activeNetworkInfo!= NULL;
}
 

(<一href="http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available">Android - 检测是否有互联网连接可用)

这也需要

 &LT;使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/&GT;
 

在你的Andr​​oidManifest.xml

现在你可以设置缓存行为要么LOAD_CACHE_ONLY或LOAD_NO_CACHE,这取决于网络连接是否可用,以

  webview.getSettings()。setCacheMode(...)
 

I am trying to get the HTML5 offline cached version of a website to display when the network is down inside of a webview.

I have overridden onReceivedError ok, and when the network is down this method is called. Problem is that it displays the generic "Web Page is not available" message.

How can I get it to display the HTML5 cached version of the page? The offline storage of the webapp is definately working, as it works fine in the desktop version of Firefox and Chrome.

I know I can call loadData into the view manually in onReceivedError, but im not sure where I can get the HTML5 cached value from.

Note: If I set some dummy data in loadData such as view.loadData(Uri.encode("<html><div>Page load failed</div></html>"), "text/html", "UTF-8"); and then click back (by detecting back event and calling webview.goBack(); then the cached version of the page is displayed ok.

Here are some lines of code I added to setup the webview:

webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webview.getSettings().setAppCacheMaxSize(1024*1024*8);                         
webview.getSettings().setAppCachePath("/data/data/com.stuff.android/cache");
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAppCacheEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setJavaScriptEnabled(true);

解决方案

Try to find out the network status using

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

(Android - detect whether there is an Internet connection available)

This also needs

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in your AndroidManifest.xml

Now you can set the cache behaviour either to LOAD_CACHE_ONLY or to LOAD_NO_CACHE, depending on whether a network connection is available, with

webview.getSettings().setCacheMode(...)

这篇关于web视图显示在android的缓存版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆