Question

I want to backup my app's data using Android backup service, but I'm concerned about network usage. I have about 500KB of data that needs to be uploaded for each backup operation. Not much, but if the backup is performed 10 times per day, it can easily reach 5MB.

My question is, when and how often does Android decides to perform backup? The documentation only mentions "at an opportune time in the future" after I call dataChanged(). It doesn't explain what conditions constitute an "opportune time".

You can request a backup operation at any time by calling dataChanged(). This method notifies the Backup Manager that you'd like to backup your data using your backup agent. The Backup Manager then calls your backup agent's onBackup() method at an opportune time in the future.

Was it helpful?

Solution

There are 2 parts to your question:

  1. When
  2. How Often

Let's start with #2, how often. I think the documentation sufficiently answers this. Any time your app calls the dataChanged() method, there is the possibility of a backup. Thus would be prudent to limit the frequency of calls to this method. It is up to you how to handle this. One approach you might want to consider it to only call this for significant/important data changes in your app. That is, do not call it for changes to user preferences (like background color e.g.), but do call it for any sort of transactional data.

As for #1, when: That is harder to answer exactly. The documentation is noncommittal about this. This gives Android the freedom to change this algorithm, without violating any developer expectations. I do think it is reasonable to assume the following: the backup is likely to happen quickly, probably at the soonest time conditions are appropriate. If it did not happen "soon", then it would not be a very useful service, would it? There are probably some simple conditions that Android waits for (just a guess but this might be available threads, available network connection with no other network activity). Note that the documentation states "it will back up using whichever transport is enabled on the device". That sounds like it is designed to make the backup happen as early as possible.

OTHER TIPS

I've dove further on researching the Android Backup Manager Service and discovered the following:

  1. According to "Requesting Backup Section" in http://developer.android.com/guide/topics/data/backup.html "If you call dataChanged() several times consecutively, before the Backup Manager requests a backup from your agent, your agent still receives just one call to onBackup()."

I interpret this as each DataChanged() call in our App notifies the Backup Manager Service via transport. The Backup Manager Service will only perform ONE call to our App's Backup Agent's onBackup(), even if DataChanged gets called several times before the Backup Manager Service responds when it wants to (see item 2 below on Backup Manager respond frequency).

  1. According to this Tester (I also ran a backup frequency test just now): https://advancedweb.hu/2014/12/09/practical_measurement_of_the_android_backup_manager/ The Backup Manager Service responds every hour (I also proved this in my tests) as long as at least one DataChanged() was called in-between the hour since the last data backup.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top