Android's WebView support HTML5 's offline application features detailed configuration _android

Source: Internet
Author: User
HTML5 's offline application features enable WebApp to function even when the network is disconnected, which is a useful feature. In the recent work also wants to use HTML5 off-line application function, because is doing on the Android platform, therefore the natural choice webview to parse the webpage. But how to enable WEBIVEW support HTML5 off-line application function, after repeatedly groping and searching for information on the Internet, repeated to do the experiment finally succeeded.

First you need to configure some of the properties of WebView, assuming that there is already a WebView instance object in the activity, named M_webview, and then add the following code:
Copy Code code as follows:

WebSettings webseting = M_webview.getsettings ();
Webseting.setdomstorageenabled (TRUE);
Webseting.setappcachemaxsize (1024*1024*8)//Set buffer size, I set the 8M
String Appcachedir = This.getapplicationcontext (). Getdir ("cache", Context.mode_private). GetPath ();
Webseting.setappcachepath (Appcachedir);
Webseting.setallowfileaccess (TRUE);
Webseting.setappcacheenabled (TRUE);
Webseting.setcachemode (Websettings.load_default);

WebView can set a Webchromeclient object that responds to the expansion buffer in its onreachedmaxappcachesize function. The code is as follows
Copy Code code as follows:

M_webview.setwebchromeclient (m_chromeclient);
Private Webchromeclient m_chromeclient = new Webchromeclient () {
Capacity to expand Cache
@Override
public void Onreachedmaxappcachesize (Long spaceneeded,
Long Totalusedquota, Webstorage.quotaupdater quotaupdater) {
Quotaupdater.updatequota (spaceneeded * 2);
}
};

Second, to modify the HTTP server configuration, so that it supports text/cache-manifest, I am using the Apache server, is the Windows version, in the Apache Conf folder to find Mime.types files, opened at the end of the file plus
"Text/cache-manifest MF manifest", restart the server. This step is very important, because I did not configure this on the server side, so it failed many times, and finally the clue found in the reply to Appendix 1.
After the above setup WebView can support the off-line application of HTML5.

Appendix Link 1 says the buffer directory should be Getapplicationcontext (). Getcachedir (). GetAbsolutePath (); But I've been experimenting to find out that the directory doesn't work, maybe it's a different version of Android. Mine is Android4.0.3, and he's probably the previous Android version.

The buffer directory uses Getapplicationcontext (). Getdir ("cache", Context.mode_private). GetPath () is a clue found in Appendix 2.

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.