Example usage for android.webkit CookieManager getInstance

List of usage examples for android.webkit CookieManager getInstance

Introduction

In this page you can find the example usage for android.webkit CookieManager getInstance.

Prototype

public static CookieManager getInstance() 

Source Link

Document

Gets the singleton CookieManager instance.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void removeAllCookies() {
    CookieManager cookieManager = CookieManager.getInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.removeAllCookies(null);
    } else {/*from   w w w .  j  a va  2  s.  co  m*/
        //noinspection deprecation
        cookieManager.removeAllCookie();
    }
}

From source file:Main.java

public static String getCookie(String siteName, String cookieName) {
    String cookieValue = null;//from   ww w. ja v  a2  s. co  m

    CookieManager cookieManager = CookieManager.getInstance();
    String cookies = cookieManager.getCookie(siteName);
    String[] temp = cookies.split("[;]");
    for (String ar1 : temp) {
        if (ar1.contains(cookieName)) {
            String[] temp1 = ar1.split("[=]");
            cookieValue = temp1[1];
        }
    }
    return cookieValue;
}

From source file:Main.java

public static void removeAllCookies(Context context) {
    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();//from ww  w  . ja  v a2s.  c  om
}

From source file:Main.java

public static String getCookieValue(Context context, String url, String key) {
    CookieSyncManager csm = CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    csm.sync();//from   ww w .j  av  a 2s.c o m
    String cookieStr = cookieManager.getCookie(url);
    System.out.println("========cookied:" + cookieStr);
    String[] strs = cookieStr.split(";");
    String value = null;
    for (String string : strs) {
        if (string.trim().startsWith(key)) {
            value = string.substring(string.indexOf("=") + 1);
            break;
        }
    }
    return value;
}

From source file:Main.java

public static void clearCookies(Context context) {
    @SuppressWarnings("unused")
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();//from  w  w w.  j ava2 s .  c o m
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static void clearCookies(Context context) {

    if (Build.VERSION.SDK_INT >= 21) {
        try {/*w  ww .  j  a v  a2  s. c om*/
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } catch (Exception e) {
        }
    } else {
        try {
            CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
            cookieSyncMngr.startSync();
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void removeAllCookiesV21() {
    final CookieManager cookieManager = CookieManager.getInstance();

    Looper looper = Looper.myLooper();// w  w  w.jav  a2 s .  c o m
    boolean prepared = false;
    if (looper == null) {
        Looper.prepare();
        prepared = true;
    }

    // requires a looper
    cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
        @Override
        public void onReceiveValue(Boolean value) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    // is synchronous, run in background
                    cookieManager.flush();
                }
            };
            thread.start();
        }
    });

    if (prepared) {
        looper = Looper.myLooper();
        if (looper != null) {
            looper.quit();
        }
    }
}

From source file:Main.java

public static void clearCookies(Context context) {
    // Edge case: an illegal state exception is thrown if an instance of 
    // CookieSyncManager has not be created.  CookieSyncManager is normally
    // created by a WebKit view, but this might happen if you start the 
    // app, restore saved state, and click logout before running a UI 
    // dialog in a WebView -- in which case the app crashes
    try {/*  w  ww  .jav  a 2  s  .  co  m*/
        @SuppressWarnings("unused")
        CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
    } catch (Exception ex) {
    }
}

From source file:Main.java

private static void clearCookiesForDomain(Context context, String domain) {
    // This is to work around a bug where CookieManager may fail to instantiate if CookieSyncManager
    // has never been created.
    CookieSyncManager syncManager = CookieSyncManager.createInstance(context);
    syncManager.sync();//from  www  . j a  v  a2s .c  o m

    CookieManager cookieManager = CookieManager.getInstance();

    String cookies = cookieManager.getCookie(domain);
    if (cookies == null) {
        return;
    }

    String[] splitCookies = cookies.split(";");
    for (String cookie : splitCookies) {
        String[] cookieParts = cookie.split("=");
        if (cookieParts.length > 0) {
            String newCookie = cookieParts[0].trim() + "=;expires=Sat, 1 Jan 2000 00:00:01 UTC;";
            cookieManager.setCookie(domain, newCookie);
        }
    }
    cookieManager.removeExpiredCookie();
}

From source file:Main.java

public static void setCookie(Context context, String url) {
    FileInputStream in = null;/*from  w  w  w.  j a v a  2s  .c  o  m*/
    try {
        in = context.openFileInput(TAXICOOKIE_FILE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (in == null) {
        Log.w(TAG, "saveCookie: Cannot open file: " + TAXICOOKIE_FILE);
    }

    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String cookieStr = null;
    try {
        cookieStr = reader.readLine();
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.d(TAG, "cookieStr: " + cookieStr);
    if (cookieStr == null) {
        return;
    }

    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeSessionCookie();
    cookieManager.setCookie(url, cookieStr);
    CookieSyncManager.getInstance().sync();
}