看看Android的WebView中是显示缓存的页面 [英] Find out if Android WebView is showing cached page

查看:406
本文介绍了看看Android的WebView中是显示缓存的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个Android应用程序,除其他外,显示了网页视图的网站。据我了解,web视图会自动显示如果连接不能建立网页的缓存版本。

I'm making an Android application that, among other things, shows websites in a webview. As far as I understand, the webview automatically shows a cached version of the page if a connection can't be established.

有什么办法,以找出是否所示的页面已经从服务器或缓存中提取?

Is there any way to find out if the page shown has been fetched from the server or cache?

也许缓存的页面,甚至几岁。

Maybe even how old the cached page is.

这是为了能够,如果他/她正在观看旧的信息来通知用户。

This is to be able to notify the user if he/she is viewing old information.

推荐答案

您可以尝试破解 - 在第一盘的WebView的缓存模式 WebSettings.NO_CACHE 并加载URL。然后,创建一个自定义WebChromeClient是这样的:

You could try a hack - at first set WebView's cache mode to WebSettings.NO_CACHE and load the URL. Then, create a custom WebChromeClient like this:

final String urlToLoad = "http://www.my-url.com"; 
webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl)
    {
        if (view.getSettings().getCacheMode() == WebSettings.LOAD_NO_CACHE && urlToLoad.equals(failingUrl))
        {
            view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
            view.loadUrl(urlToLoad);
            return;
        }
        else
        if (urlToLoad.equals(failingUrl))
        {
            // cache failed as well, load a local resource as last resort
            // or inform the user
        }
        super.onReceivedError(view, errorCode, description, failingUrl);
    }
});
webview.loadUrl(urlToLoad);

这篇关于看看Android的WebView中是显示缓存的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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