peterlawn
peterlawn

Reputation: 2381

Clear cookies in Android

How can I clear all cookies in android ?

Any sample code provided will be really helpful.

Upvotes: 4

Views: 15792

Answers (4)

Daniil Pavlenko
Daniil Pavlenko

Reputation: 573

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

Upvotes: 7

esoxjem
esoxjem

Reputation: 163

Use a CookieManager instance to manage cookies in your app. Probably in a custom Application class.

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

And whenever you want to clear, call a method like the one below:

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

Upvotes: 1

Cassio Landim
Cassio Landim

Reputation: 1946

    CookieSyncManager.createInstance(this); 
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookies(callback);

Upvotes: 34

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

Go to browser, click a Menu button, More, Preferences, Clear cookies

Upvotes: -8

Related Questions