如何清除网页视图的饼干和缓存在Android上,不用时的WebView? [英] How to clear cookies and cache of webview on Android when not in webview?

查看:399
本文介绍了如何清除网页视图的饼干和缓存在Android上,不用时的WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户登出我的应用我清除可能已通过调用此方法缓存previously从网页流量的一切:

Upon a user's sign out from my app I am clearing everything that may have been cached previously from the webview by calling this method:

 public void clearCookiesAndCache(Context context){
    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.removeAllCookies(null);
    }
    else {
        cookieManager.removeAllCookie();
    }        
}

CookieSyncManager但是被标记为德precated。 CookieSyncManager.createInstance(上下文)需要被调用,但是,如果你还没有加载web视图previously。那么,我们应该如何清除cookies和高速缓存不使用的情况下pcated CookieSyncManager去$ P $其中的WebView可能不是previously装?

CookieSyncManager is marked as deprecated, however. CookieSyncManager.createInstance(context) is necessary to be called, however, if you have not loaded the webview previously. So how are we supposed to clear the cookies and cache without using the deprecated CookieSyncManager in cases where the webview may not have been previously loaded?

推荐答案

我用我的应用程序下面的办法:

I use the following approach in my app:

    @SuppressWarnings("deprecation")
    public static void ClearCookies(Context context)
    {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            Log.d(C.TAG, "Using ClearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } else
        {
            Log.d(C.TAG, "Using ClearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
            CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
            cookieSyncMngr.startSync();
            CookieManager cookieManager=CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        }
    }

我拨打以下方式这个方法从我的片段:

I call this method in the following manner from my fragment:

mWebView.clearCache(true);
mWebView.clearHistory();

U.ClearCookies(getActivity());

mWebView.loadUrl(authorizeURL);

这是可能前和

String yahooCookies = CookieManager.getInstance().getCookie("https://yahoo.com");
Log.d(C.TAG, "Cookies for yahoo.com:" + yahooCookies);

呼叫后 ClearCookies yahooCookies将

After calling ClearCookies yahooCookies will be null.

本实施饲料我的需求,我已经测试它在几个仿真器和prehistoric三星Galaxy吉奥采用Android 2.3.3和Nexus 5采用Android 5.1.1。

This implementation feeds my needs, I have tested it on several emulators and a prehistoric Samsung Galaxy Gio with Android 2.3.3 and Nexus 5 with Android 5.1.1.

这篇关于如何清除网页视图的饼干和缓存在Android上,不用时的WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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