我尝试从WebView
删除Cookie并清除历史记录,但无法完全注销。
@Override
public void onPageFinished(WebView view, String url) {
view.clearHistory();
super.onPageFinished(view, url);
String title = mWebView.getTitle();
if (title != null && title.length() > 0) {
mTitle.setText(title);
}
CookieSyncManager.createInstance(getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.setAcceptCookie(false);
Log.d(TAG, "onPageFinished URL: " + url);
mSpinner.dismiss();
}
并设置WebView
private void setUpWebView() {
mWebView = new WebView(getContext());
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(new OAuthWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setSavePassword(false);
mWebView.clearView();
mWebView.loadUrl("about:blank");
mWebView.loadUrl(mUrl);
mWebView.clearCache(true);
mWebView.clearHistory();
mWebView.setLayoutParams(FILL);
mContent.addView(mWebView);
CookieSyncManager.createInstance(getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
//cookieManager.setAcceptCookie(false);
}
我做错了吗?
请帮助我,提前致谢。
答案 0 :(得分:1)
CookieSyncManager
被标记为已弃用,
这是方法
public abstract void removeAllCookies(ValueCallback callback)
在你的情况下:
cookieManager.removeAllCookies(null);
应该是这样的
android.webkit.CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// for >21 devices
cookieManager.removeAllCookies(null);
}
else
{
// for <21 devices
cookieManager.removeAllCookie();
}
如果使用&#34; null&#34;它不起作用,那么你必须使用回调*。如下所示
android.webkit.CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// for >21 devices
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
{
// for <21 devices
cookieManager.removeAllCookie();
}
希望这会有所帮助。
答案 1 :(得分:1)
使用以下方法清除每次登录的cookie。 API级别21中已弃用removeAllCookie
方法。请改用removeAllCookies(ValueCallback)
。
// Clear cookie to prevent automatic login
@SuppressWarnings("deprecation")
private void clearCookie() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().removeAllCookies(null);
} else {
CookieManager.getInstance().removeAllCookie();
}
}
这也应该有用
WebSettings webSettings = webview.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webView.clearCache(true);
webView.clearHistory();