1

My app create a few files dynamically and keeps track of the files through an object. At some point the object is passed to an IntentService. The purpose of the IntentService is to

  • Create a XML-file to contain data about the other files
  • Add the XML-file and the other files to a ZIP-file
  • Upload the ZIP-file to a WCF-service

The ZIP-file MUST be uploaded at some point. Things can happen; the WCF-service may be down and so on.

What options do I have in terms of guaranteeing that ZIP-file will be uploaded? My current plan is to save some info to a SQLite DB and check it every time the app is started and take it from there. Not sure that is a good idea or if there are better ways to handle this.

Also, I am an Android newbie, but if I have understood things correctly, if the IntentService is shut down before it is done, it will automatically be re-created with the same Intent and such at some point, right? Else I will have to take that into account as well.

user1323245
  • 638
  • 1
  • 8
  • 20

2 Answers2

2

You should take a look at using the built in sync service by making use of a sync adapter Have a look at this http://naked-code.blogspot.co.uk/2011/05/revenge-of-syncadapter-synchronizing.html

Watch the google I/O video embedded into that page.

You should take note that Virgil is suggesting that to use anything other than a sync adapter is just plain wrong. SyncAdapters are simple to implement and the link above also demonstrated how to set one up.

A SyncAdapter will take care of re-try attempts and will choose the optimal moment to sync data for your phone. You can schedule sync adapters or you can request an immediate sync, whatever suits your needs.

names and passwords are not saved by the account manager. They will; be needed by your website or an oauth2 service but the account manager stores the token not credcentials. If the token needs refreshing then the user is shown the account manager login screen and your code is then run to fetch a new auth token.

See http://developer.android.com/training/id-auth/identify.html for more details

jamesc
  • 12,423
  • 15
  • 74
  • 113
  • The SyncAdapter seems to be a really nifty option. Unfortunately I cannot allow the user to save any kind of username/password, and a username/password is needed to be able to upload the file. I guess that rules out using a SyncAdapter? – user1323245 May 02 '13 at 14:37
0

My current plan is to save some info to a SQLite DB and check it every time the app is started and take it from there.

You're almost there. Instead of checking the DB every time the app starts, use an AlarmManager and check the status of the upload every X hours / days. The upload can be initiated by an IntentService.

The reason an AlarmManager is better is that the user may choose to keep the app open for days or weeks ! With a repeated alarm that runs your database and upload code, you can be certain that the upload attempt will happen at regular intervals.

Take a look at this related question on scheduling recurring tasks - Scheduling recurring task in Android

Community
  • 1
  • 1
Deepak Bala
  • 11,095
  • 2
  • 38
  • 49
  • Using an alarm manager on it's own would work but it is the wrong approach as the phone could well be busy with other network traffic. Using a SyncAdapter in conjunction with an alarm manager could be a better solution but a SyncAdapter has a built in scheduling option anyway so it;s not necessary. – jamesc Apr 08 '13 at 15:02
  • I wouldn't call it wrong. Turning off 'global sync' will make sure that the sync adapter is never invoked. I agree that using the adapter in conjunction with [managing the network data used by the app](http://developer.android.com/training/basics/network-ops/managing.html) is a better solution, provided sync is turned on. – Deepak Bala Apr 08 '13 at 15:44
  • I totally disagree. You should respect your users wishes. If your app doesn't respect the global sync settings then it is indeed wrong. If I switch my sync settings off, then it means I don't want any network traffic, maybe because I am using a roaming agreement and network traffic is going to cost a lot of money, or many other reasons such as needing to reserve some credit on PAYG and no WiFi available. Sorry, but it is just plain wrong not to use a sync adapter. – jamesc Apr 08 '13 at 15:58
  • I agree that you should respect the user's wishes too and I'm all for it. My solution to the problem is biased by the OP's statement - '**MUST** be uploaded at some point'. – Deepak Bala Apr 08 '13 at 16:18
  • A SyncAdapter will guarantee that. The next time it is appropriate to do so then data will be synced. If you request a sync when the sync settings are disabled then a sync will occur the next time they are enabled. A sync adapter will also take care of network interruptions. There is a reason why Google request that you use a SyncAdapter rather than implementing your own service. – jamesc Apr 08 '13 at 17:22