Android APIs
public 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 Methods
synchronized boolean acceptCookie()
Gets whether the application's WebView instances send and accept cookies.
static boolean allowFileSchemeCookies()
Gets whether the application's WebView instances send and accept cookies for file scheme URLs.
String getCookie(String url)
Gets the cookies for the given URL.
synchronized static CookieManager getInstance()
Gets the singleton CookieManager instance.
synchronized boolean hasCookies()
Gets whether there are stored cookies.
void removeAllCookie()
Removes all cookies.
void removeExpiredCookie()
Removes all expired cookies.
void removeSessionCookie()
Removes all session cookies, which are cookies without an expiration date.
synchronized 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.
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 Methods

public synchronized 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 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 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 synchronized boolean hasCookies ()

Added in API level 1

Gets whether there are stored cookies.

Returns
  • true if there are stored cookies

public void removeAllCookie ()

Added in API level 1

Removes all cookies.

public void removeExpiredCookie ()

Added in API level 1

Removes all expired cookies.

public void removeSessionCookie ()

Added in API level 1

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

public synchronized void setAcceptCookie (boolean accept)

Added in API level 1

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

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. 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 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 must not have expired and must not be a session cookie, otherwise it will be ignored.

Parameters
url the URL for which the cookie is 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.