2

我有一个WebView实例,它允许用户进行身份验证并访问 Web 应用程序提供的某些部分功能。一旦用户完成了他们的工作,WebView为了安全起见,我们需要清理会话。

奇怪的是,下次我打开屏幕时WebView,会话仍然存在,尽管我尝试清理会话,但我可以从中断的地方继续。

我尝试做的事情(我实际上运行了调试器以确保调用这些方法):

webView.clearCache(true)
webView.clearFormData()
webView.clearHistory()
webView.clearSslPreferences()
webView.clearMatches()

CookieManager.getInstance().removeAllCookies {
    CookieManager.getInstance().flush()
}

flush()removeAllCookies()'s 回调中调用的原因是该removeAllCookies()方法是异步的,所以我在想可能需要一些时间才能将内存中 cookie 存储的状态与持久存储同步,但这无济于事。

我没有做任何花哨的事情CookieManager,它是操作系统提供的默认设置(Android 9.0 安装在 Pixel 2 XL 上)。有什么我想念的吗?

4

2 回答 2

5

好的,原来会话存储在本地存储中,所以您需要做的就是:

WebStorage.getInstance().deleteAllData()
于 2019-07-29T17:13:56.067 回答
1

尝试这个:

@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. 
于 2019-07-29T15:42:03.440 回答