com.tencent.smtt.sdk.CookieManager Java Examples

The following examples show how to use com.tencent.smtt.sdk.CookieManager. 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: 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 #3
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 #4
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 #5
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 #6
Source File: WebViewSdkCompat.java    From appcan-android with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void setCookie(String inUrl, String cookie) {
    CookieManager.getInstance().setCookie(inUrl, cookie);
    if (Build.VERSION.SDK_INT < 21) {
        CookieSyncManager.getInstance().sync();
    } else {
        CookieManager.getInstance().flush();
    }
}
 
Example #7
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 #8
Source File: WebkitCookieUtils.java    From YCWebView with Apache License 2.0 5 votes vote down vote up
/**
 * 写入磁盘
 */
private static void flush() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        CookieManager.getInstance().flush();
    } else {
        CookieSyncManager.getInstance().sync();
    }
}
 
Example #9
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 #10
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 #11
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 #12
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 #13
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 #14
Source File: AgentWebX5Config.java    From AgentWebX5 with Apache License 2.0 5 votes vote down vote up
private static void toSyncCookies() {

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CookieSyncManager.getInstance().sync();
            return;
        }
        AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {

                CookieManager.getInstance().flush();

            }
        });
    }
 
Example #15
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 #16
Source File: AgentWebX5Config.java    From AgentWebX5 with Apache License 2.0 5 votes vote down vote up
public static void removeAllCookies(@Nullable ValueCallback<Boolean> callback) {

        if (callback == null)
            callback = getDefaultIgnoreCallback();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CookieManager.getInstance().removeAllCookie();
            toSyncCookies();
            callback.onReceiveValue(!CookieManager.getInstance().hasCookies());
            return;
        }
        CookieManager.getInstance().removeAllCookies(callback);
        toSyncCookies();
    }
 
Example #17
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 #18
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 #19
Source File: X5WebUtils.java    From YCWebView with Apache License 2.0 4 votes vote down vote up
/**
 * 清除cookie操作
 * @param context               上下文
 */
public static void removeCookie(Context context){
    CookieSyncManager.createInstance(context);
    CookieSyncManager.getInstance().startSync();
    CookieManager.getInstance().removeSessionCookie();
}
 
Example #20
Source File: WebkitCookieUtils.java    From YCWebView with Apache License 2.0 4 votes vote down vote up
/**
 * 清除cookie操作,所有的
 * @param context                       上下文
 */
public static void removeCookie(Context context){
    CookieSyncManager.createInstance(context);
    CookieSyncManager.getInstance().startSync();
    CookieManager.getInstance().removeSessionCookie();
}
 
Example #21
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);
}
 
Example #22
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 #23
Source File: WebViewSdkCompat.java    From appcan-android with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static void initTencentX5(final Context context) {
    int tbsVersion = 0;
    boolean noTencentX5 = false;
    try {
        String[] lists  = context.getAssets().list("widget");
        for (int i = 0; i < lists.length; i++) {
            if (lists[i].equalsIgnoreCase("notencentx5")) {
                noTencentX5 = true;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    //初始化X5引擎SDK
    tbsVersion = QbSdk.getTbsVersion(context);
    if (noTencentX5 || (tbsVersion > 0 && tbsVersion < 30000)) {
        BDebug.i("AppCanTBS", "QbSdk.forceSysWebView()");
        QbSdk.forceSysWebView();
    }

    if(!QbSdk.isTbsCoreInited() && (tbsVersion == 0 || tbsVersion >= 30000) && !noTencentX5){
        final long timerCounter = System.currentTimeMillis();
        // 如果手机没有可以共享的X5内核,会先下载并安装,首次启动不会使用X5,再次启动才会使用X5;
        // 如果手机有可以共享的X5内核,但未安装,会先安装,首次启动不会使用X5,再次启动才会使用X5;
        // 如果手机有可以共享的X5内核,已经安装,首次启动会使用X5;
        QbSdk.initX5Environment(context, new QbSdk.PreInitCallback(){
            @Override
            public void onViewInitFinished(boolean success) {
                CookieSyncManager.createInstance(context);
                CookieManager.getInstance().setAcceptCookie(true);
                CookieManager.getInstance().removeSessionCookie();
                CookieManager.getInstance().removeExpiredCookie();
                float deltaTime = (System.currentTimeMillis() - timerCounter);
                BDebug.i("AppCanTBS", "success " + success + " x5初始化使用了" + deltaTime + "毫秒");
            }

            @Override
            public void onCoreInitFinished() {
                BDebug.i("AppCanTBS", "onX5CoreInitFinished!!!!");
            }
        });
    }
}
 
Example #24
Source File: CrazyDailyWebView.java    From CrazyDaily with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("all")
private void init(Context context) {
    WebSettings setttings = getSettings();
    setttings.setJavaScriptEnabled(true);//打开js
    setttings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//设置布局
    setttings.setDomStorageEnabled(true);//打开Dom Storage
    setttings.setDatabaseEnabled(true);//打开Database
    setttings.setAppCacheEnabled(true);//打开App Cache
    setttings.setAppCacheMaxSize(Long.MAX_VALUE);
    File cacheDir = new File(context.getExternalCacheDir(), CacheConstant.CACHE_DIR_WEB);
    if (!cacheDir.exists()) {
        cacheDir.mkdirs();
    }
    setttings.setAppCachePath(cacheDir.getAbsolutePath());//设置App Cache缓存目录
    setttings.setSupportMultipleWindows(false);//不支持多窗口
    setttings.setJavaScriptCanOpenWindowsAutomatically(true);//支持js打开新窗口
    setttings.setAllowFileAccess(true);//启用WebView访问文件数据
    setttings.setSupportZoom(true);//支持缩放
    setttings.setDisplayZoomControls(false);//隐藏webview缩放按钮
    setttings.setBuiltInZoomControls(true);//支持手势缩放
    setttings.setLoadWithOverviewMode(true);//缩放至屏幕大小
    setttings.setUseWideViewPort(true);//调整屏幕自适应
    setttings.setDefaultTextEncodingName("utf-8");//设置编码格式为utf-8
    setttings.setLoadsImagesAutomatically(true);//支持自动加载图片
    setttings.setSavePassword(false);//禁止密码保存在本地
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // https中支持访问http
        setttings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // 支持第三方的cookie同步
        CookieManager.getInstance().setAcceptThirdPartyCookies(this, true);
    }
    String ua = setttings.getUserAgentString();
    setttings.setUserAgentString(String.format("%s CrazyDaily %s", ua, DeviceUtil.getVersionName()));//重置ua
    setWebViewClient(new CrazyDailyWebViewClient());
    setWebChromeClient(new CrazyDailyWebChromeClient());
    setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
            if (mDownloadCallback != null) {
                mDownloadCallback.onDownload(url, contentLength);
            }
        }
    });
}
 
Example #25
Source File: WebViewSdkCompat.java    From appcan-android with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void clearCookie() {
    CookieManager.getInstance().removeAllCookie();
}
 
Example #26
Source File: WebViewSdkCompat.java    From appcan-android with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static String getCookie(String inUrl) {
    return CookieManager.getInstance().getCookie(inUrl);
}