如何清理在 WebView 中创建的会话?
How to clean up the session created in a WebView?
我有一个 WebView
实例,它允许用户进行身份验证并访问网络应用程序提供的部分功能。用户使用 WebView
完成工作后,出于安全目的,我们需要清理会话。
怎么了运行ge,下次我用 WebView
打开屏幕时,会话仍然存在,我可以从中断的地方继续,尽管我尝试清理会话.
我尝试做的事情(我实际上 运行 调试器以确保调用这些方法):
webView.clearCache(true)
webView.clearFormData()
webView.clearHistory()
webView.clearSslPreferences()
webView.clearMatches()
CookieManager.getInstance().removeAllCookies {
CookieManager.getInstance().flush()
}
我在 removeAllCookies()
的回调中调用 flush()
的原因是 removeAllCookies()
方法是异步的,所以我在想可能需要一些时间来同步具有持久存储的内存中 cookie 存储的状态,但这没有帮助。
我没有对 CookieManager
做任何花哨的事情,它是 OS 提供的默认设置(Android 9.0 安装在 Pixel 2 XL 上)。有什么我遗漏的吗?
试试这个:
@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();
}
}
//Use above
webView.clearCache(true);
webView.clearFormData();
webView.clearHistory();
webView.clearSslPreferences()
webView.clearMatches();
clearCookies(this);
它仍然无法正常工作,然后试试这个技巧
首先检索您的首选项值的实例
//Object holding preference values before clearing data
Map<String, ?> allEntries = mySharedPreferece.getAll();
清除您的应用数据
//Call this function to clear Apps data
public void clearApplicationData() {
File cacheDirectory = getCacheDir();
File applicationDirectory = new File(cacheDirectory.getParent());
if (applicationDirectory.exists()) {
String[] fileNames = applicationDirectory.list();
for (String fileName : fileNames) {
if (!fileName.equals("lib")) {
deleteFile(new File(applicationDirectory, fileName));
}
}
}
}
public static boolean deleteFile(File file) {
boolean deletedAll = true;
if (file != null) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
}
} else {
deletedAll = file.delete();
}
}
return deletedAll;
}
恢复首选项
//Call this function to re-save the preference values
public void restorePreference() {
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());
mySharedPreferece.edit().putString(entry.getKey(),entry.getValue().toString());
}
mySharedPreferece.edit().commit();
}
然后这样使用
clearApplicationData();
restorePreference();
//At this point apps data should be cleared leaving just the preference.
好的,原来会话存储在本地存储中,所以您需要做的就是:
WebStorage.getInstance().deleteAllData()
我有一个 WebView
实例,它允许用户进行身份验证并访问网络应用程序提供的部分功能。用户使用 WebView
完成工作后,出于安全目的,我们需要清理会话。
怎么了运行ge,下次我用 WebView
打开屏幕时,会话仍然存在,我可以从中断的地方继续,尽管我尝试清理会话.
我尝试做的事情(我实际上 运行 调试器以确保调用这些方法):
webView.clearCache(true)
webView.clearFormData()
webView.clearHistory()
webView.clearSslPreferences()
webView.clearMatches()
CookieManager.getInstance().removeAllCookies {
CookieManager.getInstance().flush()
}
我在 removeAllCookies()
的回调中调用 flush()
的原因是 removeAllCookies()
方法是异步的,所以我在想可能需要一些时间来同步具有持久存储的内存中 cookie 存储的状态,但这没有帮助。
我没有对 CookieManager
做任何花哨的事情,它是 OS 提供的默认设置(Android 9.0 安装在 Pixel 2 XL 上)。有什么我遗漏的吗?
试试这个:
@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();
}
}
//Use above
webView.clearCache(true);
webView.clearFormData();
webView.clearHistory();
webView.clearSslPreferences()
webView.clearMatches();
clearCookies(this);
它仍然无法正常工作,然后试试这个技巧
首先检索您的首选项值的实例
//Object holding preference values before clearing data
Map<String, ?> allEntries = mySharedPreferece.getAll();
清除您的应用数据
//Call this function to clear Apps data
public void clearApplicationData() {
File cacheDirectory = getCacheDir();
File applicationDirectory = new File(cacheDirectory.getParent());
if (applicationDirectory.exists()) {
String[] fileNames = applicationDirectory.list();
for (String fileName : fileNames) {
if (!fileName.equals("lib")) {
deleteFile(new File(applicationDirectory, fileName));
}
}
}
}
public static boolean deleteFile(File file) {
boolean deletedAll = true;
if (file != null) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
}
} else {
deletedAll = file.delete();
}
}
return deletedAll;
}
恢复首选项
//Call this function to re-save the preference values
public void restorePreference() {
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());
mySharedPreferece.edit().putString(entry.getKey(),entry.getValue().toString());
}
mySharedPreferece.edit().commit();
}
然后这样使用
clearApplicationData();
restorePreference();
//At this point apps data should be cleared leaving just the preference.
好的,原来会话存储在本地存储中,所以您需要做的就是:
WebStorage.getInstance().deleteAllData()