Android WebView (vi) Download and cache

Source: Internet
Author: User


webview DownloadSometimes the page you load has a download link, and if you need to respond to a click Download, you need to do the following:
Webview.setdownloadlistener (New Downloadlistener () {@Overridepublic void Ondownloadstart (string url, string useragent , string contentdisposition, String Mimetype,long contentlength) {if (URL! = null && url.startswith ("http")) { StartActivity (New Intent (Intent.action_view, Uri.parse (URL)));}});

The above code is to call the system browser to download, anyway, we can monitor the download of the event by setting Downloadlistener, and get to download the link URL, so we can fully open the thread itself to download, specifically how to download I will not say more.

WebView CacheThis can be a lot of traffic and time if the data is webview every time the request is reloaded. WebView provides a caching mechanism, and the file structure under Data/data is as follows: The URL record we requested is saved in webviewcache.db, and the content of the URL is saved under the Webviewcache folder.


There are two kinds of caches in WebView: The Web Data cache (DOM Storage), the H5 cache (that is, AppCache).

AppCache allows us to have a choice of buffering all things in a Web browser, from pages, images to scripts, CSS, and so on. This is especially useful when it comes to CSS and JavaScript files that are applied to multiple pages on a Web site. There is usually a limit to its size. On Android, you need to manually turn on (setappcacheenabled), and set the path (Setappcachepath) and Capacity (Setappcachemaxsize) in Android WebKit use a DB file to save AppCache data (my_path/applicationcache.db)

DomstorageStore some simple key/value on the data. Depending on the scope of the function, there are two kinds of session storage and local storage, respectively, for conversation-level storage (page close disappears)
and localized storage (the data will never expire unless it is actively deleted). In Android you can manually turn on Dom Storage (setdomstorageenabled) and set the storage path (SetDatabasePath)
Android WebKit will generate two files for Dom storage (My_path/localstorage/http_h5.m.taobao.com_0.localstorage and My_path/localstorage /DATABASES.DB).

5 Types of cache modes
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.
Deprecated in Load_cache_normal:api level 17, starting from API level 11 with 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.


Android WebView (vi) Download and cache

Related Article

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.