LoginSignup
32
29

More than 5 years have passed since last update.

【Android】WebViewでキャッシュやクッキー削除をする

Posted at

キャッシュの削除方法

webView.clearCache(true);

ちなみにキャッシュを使わないで常に表示を行いたい時は

webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

クッキーの削除

Lollipop以降と前で推奨されるメソッドが変わるので処理を分けます

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   CookieManager.getInstance().removeAllCookies(null);
   CookieManager.getInstance().flush();
} else {
   CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(getApplicationContext());
   cookieSyncMngr.startSync();
   CookieManager cookieManager=CookieManager.getInstance();
   cookieManager.removeAllCookie();
   cookieManager.removeSessionCookie();
   cookieSyncMngr.stopSync();
   cookieSyncMngr.sync();
}
32
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
29