在用户从我的应用程序注销后,我通过调用此方法清除之前可能从webview缓存的所有内容:
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);
可以在调用clearCookies
之前和之后转储域的cookie
String yahooCookies = CookieManager.getInstance().getCookie("https://yahoo.com");
Log.d(C.TAG, "Cookies for yahoo.com:" + yahooCookies);
调用clearCookies
后,yahooCookies将为null
。
这个实现满足了我的需求,我已经在几个模拟器和一个带有Android 2.3.3的史前三星Galaxy Gio和带有Android 5.1.1的Nexus 5上对其进行了测试。
工作和测试…
android.webkit.CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
// a callback which is executed when the cookies have been removed
@Override
public void onReceiveValue(Boolean aBoolean) {
Log.d(TAG, "Cookie removed: " + aBoolean);
}
});
}
else cookieManager.removeAllCookie();
要从Webview中清除所有存储的信息,
// Clear all the Application Cache, Web SQL Database and the HTML5 Web Storage
WebStorage.getInstance().deleteAllData();
// Clear all the cookies
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
webView.clearCache(true);
webView.clearFormData();
webView.clearHistory();
webView.clearSslPreferences();