应用里,如果用户退出登陆了,而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.