CookieSyncManager 现在已弃用,我可以使用什么代替? [英] CookieSyncManager is now deprecated, what can I use instead?

查看:30
本文介绍了CookieSyncManager 现在已弃用,我可以使用什么代替?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用了一个 cookie,它在所有浏览器中都可以正常工作,但是在 android 设备中,cookie 设置的速度没有我想要的那么快,保存 cookie 需要一些时间,当我删除 cookie 时也会发生同样的情况曲奇饼.有什么我可以做的让它更好地工作吗?预先感谢您的回答.

I'm using a cookie in my app which works fine in all browsers, but in android device the cookie is not setting as fast as I wanted, it takes some time until cookie is saved, same is happening when I delete the cookie. Is there anything I can do to make it work better? Thank in advance for your answers.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true); // enable javascript

    CookieManager.setAcceptFileSchemeCookies(true);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.acceptCookie();
    String cookie = CookieManager.getInstance().getCookie("mylink");

    final Activity activity = this;

    webview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
    });
    webview.loadUrl("mylink");

    setContentView(webview);
}

推荐答案

在 Lollipop 及其他版本上,CookieManager 单例本身运行良好.(参考链接 - http://developer.android.com/reference/android/webkit/CookieManager.html)然而,在 Lollipop 之前,它还需要使用来自 CookieSyncManager 的额外静态方法.在 WebView 上设置 cookie 时,以下代码适用于所有 Android 版本 -

On Lollipop and beyond, the CookieManager singleton works fine by itself. (Refer Link - http://developer.android.com/reference/android/webkit/CookieManager.html) however, prior to Lollipop it also required the use of an additional static method from CookieSyncManager. The code below works for me on all Android versions when setting the cookies on a WebView -

CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    CookieSyncManager.createInstance(this);
}
cookieManager.setAcceptCookie(true);

这篇关于CookieSyncManager 现在已弃用,我可以使用什么代替?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆