Android reads WebView cache and cleans up WebView cache

Source: Internet
Author: User
Tags sqlite database

There are two kinds of caches in WebView: Web data caching (storing open pages and resources), H5 caching (i.e. AppCache).

first, the Web cache
1. Cache composition
/data/data/package_name/cache/
/data/data/package_name/database/webview.db
/data/data/package_name/database/webviewcache.db

The WebView control is often used in projects, and when an HTML page is loaded, the database and cache two folders are generated in the/data/data/app package directory as shown in the following example:


The requested URL record is saved in Webviewcache.db, and the content of the URL is saved under the Webviewcache folder.

So just remove the files from these folders when you clean it up.

Real machine test I use the charm of the Blue 5.1 system,/data/data/application Package directory can not find the database file with the following two sentences to clean up the WebView page cache. But each execution is reported false,

Context.deletedatabase ("webview.db");   
Context.deletedatabase ("webviewcache.db");

2. Cache mode (5 types)

load_cache_only: do not use the network, only read local cache data

Load_default: Decide whether to fetch data from the network according to Cache-control.

Load_cache_normal:api level has been deprecated, starting with the API level to function in the same load_default mode

Load_no_cache: Do not use caching, only get data from the network.

load_cache_else_network, the data in the cache is used whenever there is a local, regardless of whether it expires, or No-cache.
such as: www.taobao.com Cache-control for No-cache, in the mode Load_default, regardless of the data will be taken from the network, if there is no network, there will be error page; in Load_cache_else_ In network mode, the cache is used whenever there is a local cache, regardless of whether there are networks. The local cache is not available from the network.
The Cache-control for www.360.com.cn is max-age=60, and local cache data is used in both modes.

Summary: According to the above two modes, the proposed cache strategy is to determine if there is a network, if so, use Load_default, no network, use Load_cache_else_network.


and databases in the webviewcache.db in the picture address and picture name corresponding information such as table ~ Export can also be used SQLite Database Browser tools to view

1. Priority caching

Well, here's what you want to ask: since these pictures already exist in the cell phone cache, why WebView can't show it again?

Here we need to set the following:

WebSettings websettings= webview.getsettings ();

Websettings.setcachemode (websettings.load_cache_else_network);

// webview.getsettings (). Setcachemode (Websettings.load_no_cache); cache is not used by default!

The meaning of Load_cache_else_network is:

Use of the cache if content is there, even if expired (eg, history nav) if it isn't in the cache, load from network. Use with setCacheMode(int) .

If the content already exists, the cache uses the cache, even if it is past history. If it doesn't exist in the cache, get it from the network!

So add this sentence, not only can use the cache offline to display the user browsed content, but also in the case of a network priority to call the cache, to reduce traffic for users! ~


2. Cache Management:

(1) Clearcachefolder (Activity.getcachedir (), System.currenttimemillis ());//delete the cache before this time.

Execute the following code before exiting the program:

File File = Cachemanager.getcachefilebasedir ();    if (file!)  = null && file.exists () && file.isdirectory ()) {for (file Item:file.listFiles ()) {item.delete ();    }    File.delete ();    }    context.deletedatabase ("webview.db");    context.deletedatabase (" Webviewcache.db ");


For more examples, please refer to: http://www.android100.org/html/201409/07/63446.html

Second, H5 cache

I haven't dabbled in this yet. Please refer to: http://87426628.blog.163.com/blog/static/6069361820139183417725/

Reference:

http://androiddada.iteye.com/blog/1280946

If the offline load appears garbled can be consulted: http://androidturing.iteye.com/blog/1280656

http://androiddada.iteye.com/

2. Cache Management:

(1) Clearcachefolder (Activity.getcachedir (), System.currenttimemillis ());//delete the cache before this time.

Android reads WebView cache and cleans up WebView cache

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.