Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "CookieManager::removeAllCookies(ValueCallback<Boolean> callback)"

Tags:

android

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.

like image 412
Talespin_Kit Avatar asked Feb 05 '15 19:02

Talespin_Kit


2 Answers

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();
like image 198
rouk1 Avatar answered Dec 09 '22 06:12

rouk1


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();
}
like image 35
San Avatar answered Dec 09 '22 06:12

San