开发者

Clear cookies in Android

How can I clear all cookies in android ?

Any sample code provided will be really he开发者_Python百科lpful.


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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜