AppCache correct settings for Android WebView
Asked Answered
M

1

10

I'm trying to figure out which are the correct settings to enable appcache on android webview. I found many discussions about that but none of them worked.

Given that AppCache is set correctly (it works on chrome), my wrong settings on the webview are the following:

mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache");
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
webSettings.setAppCacheEnabled(true);
webSettings.setSupportMultipleWindows(true);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.loadUrl("http://www.myapp.com");

Any idea of why it doesn't work?

Marine answered 7/5, 2015 at 0:43 Comment(2)
Can you please describe what is not working? What are you expecting, and what is happening instead?Sike
What I expect: my app working offline. What I get: the app does not work offline, but it does correctly on mobile browsers.Marine
M
21

FOUND THE SOLUTION:

The app cache path wasn't set correctly. I'm now using the follownig code to define the path:

String appCachePath = activity.getCacheDir().getAbsolutePath();
webSettings.setAppCachePath(appCachePath);

Instead of the old version:

webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache");

Hope will be useful for other developers :)

Marine answered 8/5, 2015 at 0:1 Comment(5)
It was useful for me, thank you so much, it came in handyNurmi
webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache"); is deprecated in version 33. solution please ?Volscian
@Imrankhan what is the solution for webSettings.setAppCachePath("/data/data/"+ getPackageName() +"/cache"); because I am getting error here, please do some help?Gertie
@ninja use the webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);Volscian
Oka, I have done like thatGertie

© 2022 - 2024 — McMap. All rights reserved.