Reputation: 2381
How can I clear all cookies in android ?
Any sample code provided will be really helpful.
Upvotes: 4
Views: 15792
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
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
Reputation: 1946
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(callback);
Upvotes: 34
Reputation: 43098
Go to browser, click a Menu button, More, Preferences, Clear cookies
Upvotes: -8