Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has anyone successfully limited the size of their WebView's cache?

I'm creating a news reader app that, at times, loads web pages into a WebView.

I noticed that with default behavior, the cache can get very unreasonable very quickly. Whether or not Android will clear out that cache when memory gets low, I'm not okay with my users seeing a 15MB news app when everything except the web cache is about 1MB.

However, I would love to use the cache to some extent. I saw WebSettings#setAppCacheMaxSize() and it seemed like it was just the ticket.

It doesn't seem like it does anything. I hardcoded the following into a method that is called by my activity's onCreate(), before anything is loaded:

originalSourceWebView.getSettings().setAppCacheMaxSize(300*1024); (I was using a larger, configurable value but dropped it lower to confirm my problem).

I blow right by 300KB without any fanfare, as shown by settings->applications->manage applications->click on my app. I'm listening in WebChromeClient#onReachedAppMaxCacheSize for events, but that never seems to trigger.

Has anyone successfully used this? Is there some different cache I should be trying to manage? The docs seem pretty sparse. I went into the android source but it just sends values off to native code, and I don't know how to delve into that.

My test environment is a nexus one on 2.2.2. I know that I'll need to make the cache calls by reflection in order to support 1.5 and 1.6, but if I can't make this work, there will be no need.

like image 373
MaximumGoat Avatar asked Feb 17 '11 02:02

MaximumGoat


1 Answers

I don't think there's an API to do what you want.

setAppCacheMaxSize limits the cache size for the HTML5 application cache API -- the offline storage that HTML 5 apps can use while running in your WebView -- but it sounds like you want to limit the cache used for loading pages. images, etc.

As a workaround you could call WebView.clearCache periodically, although that really limits the effectiveness of the cache. If you want to try and keep some recent pages around, you could look into getting them from CacheManager before clearing the cache and then restoring them afterwards.

like image 199
Mike Avatar answered Nov 10 '22 13:11

Mike