Download large application data pattern
I am developing a map rendering application for Android. The map data is quite big about 1.1 Gb. Since there are limits both in the market and in the phone for .apk size the recommendations is to download it when application starts.
This is the pattern I use now:
1. Main activity (used for rendering the map) checks if the data is avaiable, if not i starts a开发者_StackOverflow中文版 download activity 2. The download activity starts a local service 3. The service downloads the zip files and unpacks the mapdata 4. The service sends events to the download activity and notificationsI spawn a service since the whole process can take some time from 30min to a couple of hours depending on phone and network.
But I have trouble making sure that only one instance of the program is running, I have tried all combinations of the launchmode property to no help.
As you may understand two instances running will crash the download/unzip.
This is really frustrating! Now I am doing some ugly read/write to sdcard file lock and it is working most of the time to make sure only one instance is running.
I am targeting Android 1.6 and above.
To avoid problems with multiple instances, you should use remote service instead of local service. This way, you can have only one instance of the service which will download data you need for the application.
1.1GB to a mobile device is a huge amount of data. Even over wifi, unless the connection is pretty good it's going to take time, as you said, and also affect the phone's performance. I'd expect the user's experience to be bad - their phone is going to be slow during the download, probably unusable during the unpacking of so much data, and the app itself won't be usable at all until the data has been downloaded in full (which may take several retries).
If it were me I'd be looking for a way to break the data down, perhaps by geographic region, and download it in packages. That way if a package download fails the app can retry, and the user can still, to some degree, use the app. You said your app is a mapping app, so perhaps you can identify the user's location/country and download the most suitable package first. Whether this will work will come down to whether there's no way at all the user can make any use of the app without ALL the data.
The issue is that Android apps may be GC'd at any point, so your requirement to have an app running for a relatively long period of time doing intensive work, but in the background, are competing. All the user needs to do is open a big app like the Browser and your service may well get shut down. By having a single massive download transaction you're backing yourself into a corner.
Alternatively, could you provide the data physically on an SD card? It's not a great solution, but given the size of your dataset it seems worth considering.
精彩评论