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.