Java Code Examples for com.tencent.smtt.sdk.CookieManager#getInstance()

The following examples show how to use com.tencent.smtt.sdk.CookieManager#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CookieUtils.java    From AndroidHybridLib with Apache License 2.0 6 votes vote down vote up
private static void synCookies(Context context, WebView webView, String domain, Map<String, String> cookieMap) {
    if (cookieMap == null || TextUtils.isEmpty(domain)) {
        return;
    }
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    for (Map.Entry<String, String> entry : cookieMap.entrySet()) {
        String cookie = entry.getKey() + "=" + entry.getValue();
        cookieManager.setCookie(domain, cookie);
    }
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.setAcceptThirdPartyCookies(webView, true);
        cookieManager.flush();
    } else {
        CookieSyncManager.createInstance(context);
        CookieSyncManager.getInstance().sync();
    }
}
 
Example 2
Source File: AgentWebX5Config.java    From AgentWebX5 with Apache License 2.0 6 votes vote down vote up
public static void removeSessionCookies(ValueCallback<Boolean> callback) {

        if (callback == null)
            callback = getDefaultIgnoreCallback();
        if (CookieManager.getInstance() == null) {
            callback.onReceiveValue(new Boolean(false));
            return;
        }
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CookieManager.getInstance().removeSessionCookie();
            toSyncCookies();
            callback.onReceiveValue(new Boolean(true));
            return;
        }
        CookieManager.getInstance().removeSessionCookies(callback);
        toSyncCookies();

    }
 
Example 3
Source File: WebkitCookieUtils.java    From YCWebView with Apache License 2.0 6 votes vote down vote up
/**
 * 同步cookie
 * 建议调用webView.loadUrl(url)之前一句调用此方法就可以给WebView设置Cookie
 * @param url                   地址
 * @param cookieList            需要添加的Cookie值,以键值对的方式:key=value
 */
public static void syncCookie(Context context , String url, ArrayList<String> cookieList) {
    //初始化
    CookieSyncManager.createInstance(context);
    //获取对象
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    //移除
    cookieManager.removeSessionCookie();
    //添加cookie操作
    if (cookieList != null && cookieList.size() > 0) {
        for (String cookie : cookieList) {
            cookieManager.setCookie(url, cookie);
        }
    }
    String cookies = cookieManager.getCookie(url);
    X5LogUtils.d("WebkitCookieUtils-------"+cookies);
    flush();
}
 
Example 4
Source File: WebkitCookieUtils.java    From YCWebView with Apache License 2.0 6 votes vote down vote up
/**
 * sessionOnly 为true表示移除所有会话cookie,否则移除所有cookie
 * @param sessionOnly                   是否移除会话cookie
 */
public static void remove(boolean sessionOnly) {
    CookieManager cm = CookieManager.getInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (sessionOnly) {
            cm.removeSessionCookies(null);
        } else {
            cm.removeAllCookies(null);
        }
    } else {
        if (sessionOnly) {
            cm.removeSessionCookie();
        } else {
            cm.removeAllCookie();
        }
    }
    flush();
}
 
Example 5
Source File: ClearCacheTask.java    From Dainty with Apache License 2.0 5 votes vote down vote up
@Override
protected Boolean doInBackground(String... strings) {
    String a=strings[0].substring(1,strings[0].length()-1);
    String[] ss=a.split(", ");
    for(String s:ss){
        switch (s){
            case "1":
                //清除会话和持久态Cookies(保持网页登录状态,偏好设置)
                CookieManager cm=CookieManager.getInstance();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    cm.removeAllCookies(null);
                    cm.flush();
                } else {
                    cm.removeAllCookie();
                    CookieSyncManager.getInstance().sync();
                }
                break;
            case "2":
                deleteFile(new File(context.getDir("webview",0).getPath()+"/Cache"));
                break;
            case "3":
                deleteFile(new File(context.getDir("webview",0).getPath()+"/Local Storage"));
                break;
        }
    }
    return true;
}
 
Example 6
Source File: CrazyDailySonicRuntime.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
@Override
public boolean setCookie(String url, List<String> cookies) {
    if (!TextUtils.isEmpty(url) && cookies != null && cookies.size() > 0) {
        CookieManager cookieManager = CookieManager.getInstance();
        for (String cookie : cookies) {
            cookieManager.setCookie(url, cookie);
        }
        return true;
    }
    return false;
}
 
Example 7
Source File: AgentWebX5Config.java    From AgentWebX5 with Apache License 2.0 5 votes vote down vote up
public static void removeExpiredCookies() {
    CookieManager mCookieManager = null;
    if ((mCookieManager = CookieManager.getInstance()) != null) { //同步清除{
        mCookieManager.removeExpiredCookie();
        toSyncCookies();
    }
}
 
Example 8
Source File: AgentWebX5Config.java    From AgentWebX5 with Apache License 2.0 5 votes vote down vote up
public static void syncCookie(String url, String cookies) {

        CookieManager mCookieManager = CookieManager.getInstance();
        if (mCookieManager != null) {
            mCookieManager.setCookie(url, cookies);
            toSyncCookies();
        }
    }
 
Example 9
Source File: X5CookieManager.java    From x5webview-cordova-plugin with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public X5CookieManager(WebView webview) {
        webView = webview;
        cookieManager = CookieManager.getInstance();

        //REALLY? Nobody has seen this UNTIL NOW?
//        cookieManager.setAcceptFileSchemeCookies(true);
        cookieManager.setAcceptCookie(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            cookieManager.setAcceptThirdPartyCookies(webView, true);
        }
    }
 
Example 10
Source File: X5CookieManager.java    From cordova-plugin-x5-webview with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public X5CookieManager(WebView webview) {
    webView = webview;
    cookieManager = CookieManager.getInstance();

    //REALLY? Nobody has seen this UNTIL NOW?
    // Instead of android.webkit.CookieManager.setAcceptFileSchemeCookies
    cookieManager.setAcceptCookie(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.setAcceptThirdPartyCookies(webView, true);
    }
}
 
Example 11
Source File: X5CookieManager.java    From cordova-plugin-x5-tbs with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public X5CookieManager(WebView webview) {
    webView = webview;
    cookieManager = CookieManager.getInstance();

    //REALLY? Nobody has seen this UNTIL NOW?
//        cookieManager.setAcceptFileSchemeCookies(true);
    cookieManager.setAcceptCookie(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      cookieManager.setAcceptThirdPartyCookies(webView, true);
    }
  }
 
Example 12
Source File: WebkitCookieUtils.java    From YCWebView with Apache License 2.0 5 votes vote down vote up
/**
 * 获取url的cookie操作
 * @param context                       上下文
 * @param url                           url
 * @return
 */
public static String getCookie(Context context , String url){
    CookieManager cookieManager = CookieManager.getInstance();
    String cookieStr = cookieManager.getCookie(url);
    X5LogUtils.i( "WebkitCookieUtils----Cookies = " + cookieStr);
    return cookieStr;
}
 
Example 13
Source File: WebkitCookieUtils.java    From YCWebView with Apache License 2.0 5 votes vote down vote up
/**
 * 移除指定url关联的所有cookie
 * @param url                           url链接
 */
public static void remove(String url) {
    CookieManager cm = CookieManager.getInstance();
    for (String cookie : cm.getCookie(url).split("; ")) {
        cm.setCookie(url, cookie.split("=")[0] + "=");
    }
    flush();
}
 
Example 14
Source File: X5CookieManager.java    From cordova-plugin-x5engine-webview with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public X5CookieManager(WebView webview) {
        webView = webview;
        cookieManager = CookieManager.getInstance();

        //REALLY? Nobody has seen this UNTIL NOW?
//        cookieManager.setAcceptFileSchemeCookies(true);
        cookieManager.setAcceptCookie(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            cookieManager.setAcceptThirdPartyCookies(webView, true);
        }
    }
 
Example 15
Source File: CrazyDailySonicRuntime.java    From CrazyDaily with Apache License 2.0 4 votes vote down vote up
@Override
public String getCookie(String url) {
    CookieManager cookieManager = CookieManager.getInstance();
    return cookieManager.getCookie(url);
}
 
Example 16
Source File: AgentWebX5Config.java    From AgentWebX5 with Apache License 2.0 4 votes vote down vote up
public static String getCookiesByUrl(String url) {
    return CookieManager.getInstance() == null ? null : CookieManager.getInstance().getCookie(url);
}