Android clears localStorage of WebView

Regarding the cache of webview, there will be different directories on different devices.

The login status of APP and the login status of H5 often cannot be synchronized for some reasons.

Therefore, every time you want to close the webview page, you can clear the data in the h5 cache.

Through analysis, commonly used storage of web pages are: cookie, localStorage, session

Finally, use the following method to clear the cache (sometimes it will fail, with a very small probability)

WebStorage.getInstance().deleteAllData();

Of course, other methods have also been tried

CookieSyncManager.createInstance(getApplicationContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.removeAllCookie();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    
    
    cookieManager.removeSessionCookies(null);
    cookieManager.removeAllCookie();
    cookieManager.flush();
} else {
    
    
    cookieManager.removeAllCookie();
    CookieSyncManager.getInstance().sync();
}
deleteFile(new File(getCacheDir().getParentFile().getAbsolutePath(),"app_webview"));



public void deleteFile(File file) {
    
    


        if (file.exists()) {
    
    
            if (file.isFile()) {
    
    
                file.delete();
            } else if (file.isDirectory()) {
    
    
                File files[] = file.listFiles();
                for (File value : files) {
    
    
                    deleteFile(value);
                }
            }
//            Timber.d("deleteFile   delete file path=" + file.getAbsolutePath()+","+file.delete());
            new SecurityManager().checkDelete(file.getAbsolutePath());
            Timber.d("deleteFile   delete file path=" + file.getAbsolutePath());
        } else {
    
    
            Timber.d("deleteFile   delete file no exists " + file.getAbsolutePath());
        }
    }

insert image description here

おすすめ

転載: blog.csdn.net/u011106915/article/details/124124562