Android APIs
public abstract class

CookieManager

extends Object
java.lang.Object
   ↳ android.webkit.CookieManager

Class Overview

Manages the cookies used by an application's WebView instances. Cookies are manipulated according to RFC2109.

Summary

Public Constructors
CookieManager()
Public Methods
abstract boolean acceptCookie()
Gets whether the application's WebView instances send and accept cookies.
abstract boolean acceptThirdPartyCookies(WebView webview)
Gets whether the WebView should allow third party cookies to be set.
static boolean allowFileSchemeCookies()
Gets whether the application's WebView instances send and accept cookies for file scheme URLs.
abstract void flush()
Ensures all cookies currently accessible through the getCookie API are written to persistent storage.
abstract String getCookie(String url)
Gets the cookies for the given URL.
synchronized static CookieManager getInstance()
Gets the singleton CookieManager instance.
abstract boolean hasCookies()
Gets whether there are stored cookies.
abstract void removeAllCookie()
This method was deprecated in API level 21. Use removeAllCookies(ValueCallback) instead.
abstract void removeAllCookies(ValueCallback<Boolean> callback)
Removes all cookies.
abstract void removeExpiredCookie()
This method was deprecated in API level 21. The WebView handles removing expired cookies automatically.
abstract void removeSessionCookie()
This method was deprecated in API level 21. use removeSessionCookies(ValueCallback) instead.
abstract void removeSessionCookies(ValueCallback<Boolean> callback)
Removes all session cookies, which are cookies without an expiration date.
abstract void setAcceptCookie(boolean accept)
Sets whether the application's WebView instances should send and accept cookies.
static void setAcceptFileSchemeCookies(boolean accept)
Sets whether the application's WebView instances should send and accept cookies for file scheme URLs.
abstract void setAcceptThirdPartyCookies(WebView webview, boolean accept)
Sets whether the WebView should allow third party cookies to be set.
abstract void setCookie(String url, String value, ValueCallback<Boolean> callback)
Sets a cookie for the given URL.
abstract void setCookie(String url, String value)
Sets a cookie for the given URL.
Protected Methods
Object clone()
Creates and returns a copy of this Object.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public CookieManager ()

Public Methods

public abstract boolean acceptCookie ()

Added in API level 1

Gets whether the application's WebView instances send and accept cookies.

Returns
  • true if WebView instances send and accept cookies

public abstract boolean acceptThirdPartyCookies (WebView webview)

Added in API level 21

Gets whether the WebView should allow third party cookies to be set.

Parameters
webview the WebView instance to get the cookie policy for
Returns
  • true if the WebView accepts third party cookies

public static boolean allowFileSchemeCookies ()

Added in API level 12

Gets whether the application's WebView instances send and accept cookies for file scheme URLs.

Returns
  • true if WebView instances send and accept cookies for file scheme URLs

public abstract void flush ()

Added in API level 21

Ensures all cookies currently accessible through the getCookie API are written to persistent storage. This call will block the caller until it is done and may perform I/O.

public abstract String getCookie (String url)

Added in API level 1

Gets the cookies for the given URL.

Parameters
url the URL for which the cookies are requested
Returns
  • value the cookies as a string, using the format of the 'Cookie' HTTP request header

public static synchronized CookieManager getInstance ()

Added in API level 1

Gets the singleton CookieManager instance. If this method is used before the application instantiates a WebView instance, CookieSyncManager.createInstance(Context) must be called first.

Returns
  • the singleton CookieManager instance

public abstract boolean hasCookies ()

Added in API level 1

Gets whether there are stored cookies.

Returns
  • true if there are stored cookies

public abstract void removeAllCookie ()

Added in API level 1

This method was deprecated in API level 21.
Use removeAllCookies(ValueCallback) instead.

Removes all cookies.

public abstract void removeAllCookies (ValueCallback<Boolean> callback)

Added in API level 21

Removes all cookies.

This method is asynchronous. 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, and in this case it is safe to call the method from a thread without a Looper.

Parameters
callback a callback which is executed when the cookies have been removed

public abstract void removeExpiredCookie ()

Added in API level 1

This method was deprecated in API level 21.
The WebView handles removing expired cookies automatically.

Removes all expired cookies.

public abstract void removeSessionCookie ()

Added in API level 1

This method was deprecated in API level 21.
use removeSessionCookies(ValueCallback) instead.

Removes all session cookies, which are cookies without an expiration date.

public abstract void removeSessionCookies (ValueCallback<Boolean> callback)

Added in API level 21

Removes all session cookies, which are cookies without an expiration date.

This method is asynchronous. 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 cookie were removed, and in this case it is safe to call the method from a thread without a Looper.

Parameters
callback a callback which is executed when the session cookies have been removed

public abstract void setAcceptCookie (boolean accept)

Added in API level 1

Sets whether the application's WebView instances should send and accept cookies. By default this is set to true and the WebView accepts cookies.

When this is true setAcceptThirdPartyCookies and setAcceptFileSchemeCookies can be used to control the policy for those specific types of cookie.

Parameters
accept whether WebView instances should send and accept cookies

public static void setAcceptFileSchemeCookies (boolean accept)

Added in API level 12

Sets whether the application's WebView instances should send and accept cookies for file scheme URLs. Use of cookies with file scheme URLs is potentially insecure and turned off by default. Do not use this feature unless you can be sure that no unintentional sharing of cookie data can take place.

Note that calls to this method will have no effect if made after a WebView or CookieManager instance has been created.

public abstract void setAcceptThirdPartyCookies (WebView webview, boolean accept)

Added in API level 21

Sets whether the WebView should allow third party cookies to be set. Allowing third party cookies is a per WebView policy and can be set differently on different WebView instances.

Apps that target KITKAT or below default to allowing third party cookies. Apps targeting LOLLIPOP or later default to disallowing third party cookies.

Parameters
webview the WebView instance to set the cookie policy on
accept whether the WebView instance should accept third party cookies

public abstract void setCookie (String url, String value, ValueCallback<Boolean> callback)

Added in API level 21

Sets a cookie for the given URL. Any existing cookie with the same host, path and name will be replaced with the new cookie. The cookie being set will be ignored if it is expired.

This method is asynchronous. 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 the cookie was set successfully. You can pass null as the callback if you don't need to know when the operation completes or whether it succeeded, and in this case it is safe to call the method from a thread without a Looper.

Parameters
url the URL for which the cookie is to be set
value the cookie as a string, using the format of the 'Set-Cookie' HTTP response header
callback a callback to be executed when the cookie has been set

public abstract void setCookie (String url, String value)

Added in API level 1

Sets a cookie for the given URL. Any existing cookie with the same host, path and name will be replaced with the new cookie. The cookie being set will be ignored if it is expired.

Parameters
url the URL for which the cookie is to be set
value the cookie as a string, using the format of the 'Set-Cookie' HTTP response header

Protected Methods

protected Object clone ()

Added in API level 1

Creates and returns a copy of this Object. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should call super.clone() to create the new instance and then create deep copies of the nested, mutable objects.

Returns
  • a copy of this object.