用户从我的应用程序注销后,我将通过调用此方法清除之前从 Web 视图中缓存的所有内容:
public void clearCookiesAndCache(Context context){
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(null);
}
else {
cookieManager.removeAllCookie();
}
}
但是,CookieSyncManager 被标记为已弃用。但是,如果您之前没有加载 webview,则需要调用 CookieSyncManager.createInstance(context)。那么,在 webview 之前可能尚未加载的情况下,我们应该如何在不使用已弃用的 CookieSyncManager 的情况下清除 cookie 和缓存呢?
我在我的应用程序中使用以下方法:
@SuppressWarnings("deprecation")
public static void clearCookies(Context context)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
Log.d(C.TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
} else
{
Log.d(C.TAG, "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
cookieSyncMngr.startSync();
CookieManager cookieManager=CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncMngr.stopSync();
cookieSyncMngr.sync();
}
}
或者在科特林中
@SuppressWarnings("deprecation")
fun clearCookies(context: Context?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null)
CookieManager.getInstance().flush()
} else if (context != null) {
val cookieSyncManager = CookieSyncManager.createInstance(context)
cookieSyncManager.startSync()
val cookieManager: CookieManager = CookieManager.getInstance()
cookieManager.removeAllCookie()
cookieManager.removeSessionCookie()
cookieSyncManager.stopSync()
cookieSyncManager.sync()
}
}
我从我的片段中按以下方式调用此方法:
mWebView.clearCache(true);
mWebView.clearHistory();
U.clearCookies(getActivity());
mWebView.loadUrl(authorizeURL);
可以在调用之前和之后转储域的 cookieclearCookies
by
String yahooCookies = CookieManager.getInstance().getCookie("https://yahoo.com");
Log.d(C.TAG, "Cookies for yahoo.com:" + yahooCookies);
打电话后clearCookies
yahooCookies 将是null
.
这个实现满足了我的需求,我已经在几个模拟器和带有 Android 2.3.3 的史前 Samsung Galaxy Gio 和带有 Android 5.1.1 的 Nexus 5 上对其进行了测试。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)