Android webview supports detailed configuration of HTML5 Offline Application functions

Source: Internet
Author: User

The HTML5 Offline Application function can make webapps work normally even when the network is disconnected. This is a very useful function. Recently, the HTML5 Offline Application function is also used. Because it is implemented on the Android platform, it is natural to choose Webview to parse web pages. However, how can we enable Webivew to support the HTML5 Offline Application function? After repeatedly exploring and searching for information on the Internet, the experiment was finally successful.

First, you need to configure some attributes of webview. Assume that the activity already has a Webview instance object named m_webview, and then add the following code:Copy codeThe Code is as follows: WebSettings webseting = m_webview.getSettings ();
Webseting. setDomStorageEnabled (true );
Webseting. setAppCacheMaxSize (1024*1024*8); // set the buffer size. I set it to 8 Mb.
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 and respond to the extended buffer in its onReachedMaxAppCacheSize function. The Code is as follows:Copy codeThe Code is as follows: m_webview.setWebChromeClient (m_chromeClient );
Private WebChromeClient m_chromeClient = new WebChromeClient (){
// Expand the cache capacity
@ Override
Public void onReachedMaxAppCacheSize (long spaceNeeded,
Long totalUsedQuota, WebStorage. QuotaUpdater quotaUpdater ){
QuotaUpdater. updateQuota (spaceNeeded * 2 );
}
};

Next, modify the configuration in the http server so that it supports text/cache-manifest. I use the apache server and the windows version, and find mime in the apache conf folder. types file. After opening the file, add
"Text/cache-manifest mf manifest", restart the server. This step is very important because the server side has not configured this, so it has failed many times. Finally, it is the clue found in the reply in Appendix 1.
After setting Webview, you can support HTML5 offline applications.

In appendix 1, the buffer directory should be getApplicationContext (). getCacheDir (). getAbsolutePath (); but after the experiment, I found that setting that directory does not work. It may be the Android version, but my version is Android4.0.3, and it may be the previous Android version.

The buffer directory uses getApplicationContext (). getDir ("cache", Context. MODE_PRIVATE). getPath () as 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.