10

I am a newbie to java and trying to remove the WebView cookies using the method CookieManager::removeAllCookies(ValueCallback callback). Not able to figure out what values has to be passed to the removeAllCookie method.

The docs https://developer.android.com/reference/android/webkit/ValueCallback.html and https://developer.android.com/reference/android/webkit/CookieManager.html#getInstance%28%29 does not say anything on how to use it.

My understanding is ValueCallback is similar to c++ templates. But not able to get why does an object is required to be passed to remove the cookies.

1
  • did you read the documentation for that method?
    – njzk2
    Feb 10, 2015 at 21:14

2 Answers 2

18

From the documentation:

If a ValueCallback is provided, onReceiveValue() will be called on the current thread's Looper once the operation is complete. The value provided to the callback indicates whether any cookies were removed. You can pass null as the callback if you don't need to know when the operation completes or whether any cookies were removed

So you can do this

CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
           @Override
           public void onReceiveValue(Boolean value) {
               Log.d(TAG, "onReceiveValue " + value);
           }
       });

or

CookieManager.getInstance().removeAllCookies(null);

This method is introduced in API level 21. You may have to give something like this if you are supporting older versions.

if(API Level >= 21){
     CookieManager.getInstance().removeAllCookies(null);
}else{
  CookieManager.getInstance().removeAllCookie();
}
1
  • in your code API denotes what? i'm trying to implement it,but shows in popup for import like Game,safety net, which one i have to choose.
    – MohanRaj S
    Sep 23, 2016 at 5:35
1

You need to call it in a thread.

private class RemoveCookiesThread extends Thread {
    private final ValueCallback<Boolean> mCallback;

    public RemoveCookiesThread(ValueCallback<Boolean> callback) {
        mCallback = callback;
    }

    public void run() {
        Looper.prepare();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CookieManager.getInstance().removeAllCookies(mCallback);
        } else {
            CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mApplication);
            cookieSyncManager.startSync();
            CookieManager.getInstance().removeAllCookie();
            cookieSyncManager.stopSync();
            mCallback.onReceiveValue(true);            
        }

        Looper.loop();
    }
}

And then start the thread:

RemoveCookiesThread thread = new RemoveCookiesThread(callback);
thread.start();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.