com.tencent.smtt.sdk.CookieSyncManager Java Examples

The following examples show how to use com.tencent.smtt.sdk.CookieSyncManager. 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
/**
 * 同步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 #3
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 #4
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 #5
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 #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: CBrowserWindow.java    From appcan-android with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    if (view == null) {
        return;
    }
    EBrowserView target = (EBrowserView) view;
    if (url != null) {
        String oUrl = view.getOriginalUrl();
        if (!mReferenceUrl.equals(url) || target.beDestroy() || !url.equals(oUrl)) {
            return;
        }
        if (!mReferenceUrl.equals(url) || target.beDestroy()) {
            return;
        }
    }
    ESystemInfo info = ESystemInfo.getIntence();

    if (!target.isWebApp()) {
        if (!info.mScaled) {
            info.mDefaultFontSize = (int) (info.mDefaultFontSize / target.getScaleWrap());
            info.mScaled = true;
        }
        target.setDefaultFontSize(info.mDefaultFontSize);
    }

    info.mFinished = true;
    view.loadUrl(EUExScript.F_UEX_DISPATCHER_SCRIPT);
    view.loadUrl(EUExScript.F_UEX_SCRIPT);
    target.onPageFinished(target, url);
    CookieSyncManager.getInstance().sync();
}
 
Example #8
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 #9
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 #10
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 #11
Source File: WebViewSdkCompat.java    From appcan-android with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void stopSync(){
    CookieSyncManager.getInstance().stopSync();
}
 
Example #12
Source File: CBrowserWindow7.java    From appcan-android with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    if (view == null) {
        return;
    }
    EBrowserView target = (EBrowserView) view;
    EBrowserWindow bWindow = target.getBrowserWindow();
    if (url != null) {
        if (url.startsWith("http")) {
            if (bWindow != null && 1 == bWindow.getWidget().m_webapp) {
                bWindow.hiddenProgress();
            }
        }
        String oUrl = view.getOriginalUrl();
        if ((!mReferenceUrl.equals(url) || target.beDestroy() || !url.equals(oUrl)) && mIsPageOnload) {
            return;
        }
    }
    mIsPageOnload = true;
    ESystemInfo info = ESystemInfo.getIntence();

    if (!target.isWebApp()) { //4.3及4.3以下手机
        int defaultFontSize = (int) (info.mDefaultFontSize / target.getScaleWrap());
        info.mScaled = true;
        target.setDefaultFontSize(defaultFontSize);
    }

    if (!info.mFinished) {
        if (WWidgetData.m_remove_loading == 1) {
            if (target.getContext()instanceof EBrowserActivity) {
                ((EBrowserActivity) target.getContext()).setContentViewVisible(200);
            }
        }
    }

    info.mFinished = true;
    target.loadUrl(EUExScript.F_UEX_DISPATCHER_SCRIPT);
    target.loadUrl(EUExScript.F_UEX_SCRIPT);
    target.onPageFinished(target, url);

    if (bWindow != null && bWindow.getWidget().m_appdebug == 1) {
        String debugUrlString = "http://"
                + bWindow.getWidget().m_logServerIp
                + ":30060/target/target-script-min.js#anonymous";
        String weinreString = "javascript:var x = document.createElement(\"SCRIPT\");x.setAttribute('src',\""
                + debugUrlString + "\"" + ");document.body.appendChild(x);";
        target.loadUrl(weinreString);
    }

    CookieSyncManager.getInstance().sync();
}
 
Example #13
Source File: AgentWebX5Config.java    From AgentWebX5 with Apache License 2.0 3 votes vote down vote up
private static void createCookiesSyncInstance(Context context) {


        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CookieSyncManager.createInstance(context);
        }
    }