当不在 webview 中时,如何在 Android 上清除 cookies 和 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();
}
}

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

85895 次浏览

I just had the same problem. I needed to delete all cookies because I didn't want to keep old ones and didn't want to use the deprecated method.

Here the documentation:

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

And here the method:

public abstract void removeAllCookies (ValueCallback callback)

In your case:

cookieManager.removeAllCookies(null);

should do the job. If with "null" it doesn't work, then you have to use a callback*...

Eventually you may find an answer here: Android WebView Cookie Problem

Hope this helps. Cheers

I'm using this...

@WorkerThread
public static void clearCache() {
new WebView(getApplicationContext()).clearCache(true);
}

Be careful to call it on a UI Thread otherwise you get an exception.

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();
}
}

Or in Kotlin

@SuppressWarnings("deprecation")
fun clearCookies(context: Context?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null)
CookieManager.getInstance().flush()
} else if (context != null) {
val cookieSyncManager = CookieSyncManager.createInstance(context)
cookieSyncManager.startSync()
val cookieManager: CookieManager = CookieManager.getInstance()
cookieManager.removeAllCookie()
cookieManager.removeSessionCookie()
cookieSyncManager.stopSync()
cookieSyncManager.sync()
}
}

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

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

U.clearCookies(getActivity());
    

mWebView.loadUrl(authorizeURL);

It is possible to dump the cookies for a domain before and after the call to clearCookies by

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

After calling clearCookies yahooCookies will be null.

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.

Working and tested ..

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();

To clear all the stored info from 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();