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);
在網上找了一些資料:
https://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();
}
另外這裏也提到:
https://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.
最後更新:2017-04-03 05:38:55