Android 的WebView使用CookieManager崩溃的问题

应用里,如果用户退出登陆了,而WebView里还没有退出登陆,这就有点蛋疼了。所以在用户退出时,要清除WebView的Cookie。但是据说调用CookieManager应用可能会崩溃。

先来看下微信是怎么做的,反编绎了下微信的apk,发现是这样的:

private void a(Activity paramActivity, String[] paramArrayOfString)
{
  Bundle localBundle = new Bundle();
  if (paramArrayOfString.length > 0) {
    localBundle.putString("scope", TextUtils.join(",", paramArrayOfString));
  }
  CookieSyncManager.createInstance(paramActivity);
  a(paramActivity, "oauth", localBundle, new f(this));
}
public final String cl(Context paramContext)
{
  CookieSyncManager.createInstance(paramContext);
  CookieManager.getInstance().removeAllCookie();
  AX(null);

在网上找了一些资料:

http://blog.csdn.net/shichaosong/article/details/7949580 里面的FackBook的代码的注释提到 CookieSyncManager如果没有创建,就有可能会导致app crash。

 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
        @SuppressWarnings("unused")
        CookieSyncManager cookieSyncMngr =
            CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
    }

另外这里也提到:
http://www.effecthub.com/topic/66 
Android: CookieManager removeAllCookie() Crash
When we use CookieManager.getInstance().removeAllCookie(); to remove cookie ,it's may crash with log fatal signal 11.
just add CookieSyncManager.createInstance(getActivity());we can avoid this crash.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值