如何在不在webview中时清除Android上的webview和缓存?

Joh*_*ock 51 android android-webview

当用户从我的应用程序退出时,我通过调用此方法清除之前从webview缓存的所有内容:

 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();
    }        
}
Run Code Online (Sandbox Code Playgroud)

但是,CookieSyncManager被标记为已弃用.但是,如果您之前未加载webview,则需要调用CookieSyncManager.createInstance(context).那么在以前没有加载webview的情况下,如何在不使用已弃用的CookieSyncManager的情况下清除cookie和缓存呢?

Ada*_*mVe 112

我在我的应用程序中使用以下方法:

    @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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我从我的片段中按以下方式调用此方法:

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

U.clearCookies(getActivity());

mWebView.loadUrl(authorizeURL);
Run Code Online (Sandbox Code Playgroud)

它可以转储一个域名的cookies前和呼叫后,clearCookies

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

在调用clearCookiesyahooCookies 之后null.

这个实现满足了我的需求,我已经在几个模拟器和史前三星Galaxy Gio与Android 2.3.3和Nexus 5与Android 5.1.1上进行了测试.

  • 你有我的+1,有关此代码的10倍,但请将ClearCookies的第一个字母改为小写(clearCookies)这是Java (4认同)
  • 为什么要检查 LOLLIPOP_MR1 (API 22) 而不是 LOLLIPOP (API 21)?所有这些 api 似乎都已添加到 API 21 中,所以我想知道是否有特殊原因要检查 22? (2认同)

Gay*_*tti 20

工作和测试..

android.webkit.CookieManager cookieManager = CookieManager.getInstance();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
         // a callback which is executed when the cookies have been removed
         @Override
         public void onReceiveValue(Boolean aBoolean) {
               Log.d(TAG, "Cookie removed: " + aBoolean);
         }
       });
}
else cookieManager.removeAllCookie();
Run Code Online (Sandbox Code Playgroud)


Sri*_*san 10

要从 Webview 中清除所有存储的信息,

// Clear all the Application Cache, Web SQL Database and the HTML5 Web Storage
    WebStorage.getInstance().deleteAllData();

    // Clear all the cookies
    CookieManager.getInstance().removeAllCookies(null);
    CookieManager.getInstance().flush();

    webView.clearCache(true);
    webView.clearFormData();
    webView.clearHistory();
    webView.clearSslPreferences();
Run Code Online (Sandbox Code Playgroud)


Pol*_*zio 7

我在用这个...

@WorkerThread
public static void clearCache() {
    new WebView(getApplicationContext()).clearCache(true);
}
Run Code Online (Sandbox Code Playgroud)

小心在 UI 线程上调用它,否则会出现异常。


fir*_*pol 4

我刚刚遇到了同样的问题。我需要删除所有 cookie,因为我不想保留旧的 cookie,也不想使用已弃用的方法。

这里是文档:

http://developer.android.com/reference/android/webkit/CookieManager.html

这里是方法:

公共抽象无效removeAllCookies(ValueCallback回调)

在你的情况下:

cookieManager.removeAllCookies(null);
Run Code Online (Sandbox Code Playgroud)

应该做这项工作。如果使用“null”不起作用,那么您必须使用回调*...

最终你可能会在这里找到答案:Android WebView Cookie Problem

希望这可以帮助。干杯