Clear cookies in Android

How can I delete all cookies in android?

Any sample code would be really helpful.

+3
source share
4 answers

Go to the browser, click the "Menu", "Advanced", "Settings", "Clear cookies"

-3
source
    CookieSyncManager.createInstance(this); 
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookies(callback);
+32
source
@SuppressWarnings("deprecation")
public void clearCookies(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { 
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    } else {
        CookieSyncManager cookieSyncMngr= CookieSyncManager.createInstance(context);
        cookieSyncMngr.startSync();
        CookieManager cookieManager= CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
        cookieSyncMngr.sync();
    }
}
+4

CookieManager . , Application.

mCookieManager = new CookieManager();
mCookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(mCookieManager); 

, , , :

public static void clearCookies() {
mCookieManager.getCookieStore().removeAll();
}
0

Source: https://habr.com/ru/post/1771671/


All Articles