Android Implementation WebView Delete cache method _android

Source: Internet
Author: User
Tags delete cache

The example in this article describes the Android implementation WebView Delete cache method. Share to everyone for your reference. Specifically as follows:

To delete a cache stored on your phone:

Clear the cache before time numdays
private int Clearcachefolder (File dir, long numdays) {
  int deletedfiles = 0 ;
  if (dir!= null && dir.isdirectory ()) {
    try {for
      (File child:dir.listFiles ()) {
        if ( Child.isdirectory ()) {
          Deletedfiles + = Clearcachefolder (child, numdays);
        }
        if (child.lastmodified () < numdays) {
          if (Child.delete ()) {
            deletedfiles++
          }
    }}} catch (Exception e) {
      e.printstacktrace ();
    }
  }
  return deletedfiles;
}

Turn off Use caching:

Priority use caching:
webview.getsettings (). Setcachemode (websettings.load_cache_else_network);
Do not use caching:
webview.getsettings (). Setcachemode (Websettings.load_no_cache);

When you exit the application, add the following code:

File File = Cachemanager.getcachefilebasedir ();
  if (file!= null && file.exists () && file.isdirectory ()) {for
  (file Item:file.listFiles ()) {
   it Em.delete ();
  }
  File.delete ();
  }
 Context.deletedatabase ("webview.db");
 Context.deletedatabase ("webviewcache.db");

I hope this article will help you with your Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.