1

I'm trying to use application cache in HTML5 for Android PhoneGap Application, but this doesn't work, it doesn't feel with ApplicationCahce Events.

function logEvent(event) {
        console.log(event.type);
    }

    window.applicationCache.addEventListener('checking', logEvent, false);
    window.applicationCache.addEventListener('noupdate', logEvent, false);
    window.applicationCache.addEventListener('downloading', logEvent, false);
    window.applicationCache.addEventListener('progress', logEvent, false);
    window.applicationCache.addEventListener('cached', logEvent, false);
    window.applicationCache.addEventListener('updateready', logEvent, false);
    window.applicationCache.addEventListener('obsolete', logEvent, false);
    window.applicationCache.addEventListener('error', logEvent, false);

Also, this code run in iOS PhoneGap and Android Browser and this link for supported platforms. Application Cahce Supported platforms

So, any suggestion it would be helpful.

Eva Dias
  • 1,709
  • 9
  • 36
  • 67
Ahmed Assaf
  • 601
  • 7
  • 25

3 Answers3

6

I believe the application cache is not enabled by default in the WebView. What you will need to do is in the Java class that extends DroidGap call:

this.appView.getSettings().setAppCacheEnabled(true);

in your onCreate() method.

You may also need to call:

this.appView.getSettings().setAppCacheMaxSize(sizeInBytes);
this.appView.getSettings().setAppCachePath(pathToCacheDir);

Please note the cache directory must exist before you call setAppCachePath().

Read up on...

http://developer.android.com/reference/android/webkit/WebSettings.html#setAppCacheEnabled(boolean) http://developer.android.com/reference/android/webkit/WebSettings.html#setAppCacheMaxSize(long) http://developer.android.com/reference/android/webkit/WebSettings.html#setAppCachePath(java.lang.String)

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • Thanks, but what is pathToCacheDir you suggest for me. – Ahmed Assaf Oct 03 '12 at 15:36
  • 1
    Either /data/data/{package name}/app_cache or /sdcard/Android/data/{package name}/app_cache seem like reasonable defaults. – Simon MacDonald Oct 04 '12 at 13:27
  • I had the same problem.When I modify my static file and reload the app,it seems like using the new static file. But the application cache event works well, it shows `noupdate`. I have tried you code,but it also doesn't works. – Dozer Apr 30 '13 at 08:19
  • Perhaps this used to be the case, but it appears to be working fine for me without this – Nico Westerdale Dec 06 '17 at 15:37
1

For pathToCacheDir, use:

String pathToCacheDir = this.getApplicationContext().getCacheDir().getAbsolutePath()
Bertl
  • 605
  • 5
  • 10
  • Where this path? , and Why this Place? – Ahmed Assaf Jan 13 '13 at 07:23
  • 1
    See [Context.getCacheDir] (http://developer.android.com/reference/android/content/Context.html#getCacheDir%28%29). From the API documentaiton: Returns the absolute path to the application specific cache directory on the filesystem. You can run `Log.d("mytest", pathToCacheDir);` to view the path in LogCat. – stianlik Mar 18 '13 at 09:41
1

did you set the correct mime-type? At first I use the '.txt' like thie:

<!DOCTYPE html>
<html manifest="cache.txt">
<head>

It works well in iOS, Chrome but doesn't work in Android!

When I set the correct mime-type,it works well~

In tomcat,the default extension was '.appcache',and the default mime-type was:

<mime-mapping>
    <extension>appcache</extension>
    <mime-type>text/cache-manifest</mime-type>
</mime-mapping>
Dozer
  • 5,025
  • 11
  • 36
  • 52