Android Backup API

2019-04-06 17:47发布

Can anyone explain what exactly the Android Backup API is used for?

I have read Using the Backup API and Data Backup from the Developer Docs, but it is still unclear to me.

When is data backed up & restored?

Specifically in these situations:

  1. A user installs my app on Device 1, data is backed up, and the user then installs my app on Device 2. Is user data from Device 1 automatically put on Device 2? If so, does this occur when the app is installed or when it is launched?

  2. My app is installed on 2 devices. When a change is made on Device 1 is it automatically made on Device 2? If not, is it at least possible to make the change on Device 2?

In one doc, it says

The backup service is not designed for synchronizing application data with other clients or saving data that you'd like to access during the normal application lifecycle. You cannot read or write backup data on demand and cannot access it in any way other than through the APIs provided by the Backup Manager.

But in another,

the Android framework helps you build rich cloud-enabled apps that sync their data to a remote web service, making sure all your devices always stay in sync

This seems like a contradiction to me.

More specifically, I want to continually sync a single database file (less than 20 KB) across devices. Is this possible with the Backup API?

2条回答
Viruses.
2楼-- · 2019-04-06 18:22

Edit: So to answer your question - no, it is not a reliable way to sync your data.

When a user purchases a new device or resets their existing one, they might expect that when Google Play restores your app back to their device during the initial setup, the previous data associated with the app restores as well. By default, that doesn't happen and all the user's accomplishments or settings in your app are lost.

As I see it your app is storing data to the cloud. You push the files to the backup servers and with the API key (that is unique to each users google account) allows you to download the backup to other devices but can only be accesses during the install of the app or by doing a manual request. The documentation states:

The backup service is not designed for synchronizing application data with other clients or saving data that you'd like to access at random during the application lifecycle.

As I understand it (without having implemented this my self) you cannot use this as a reliable method for cloud syncing real-time data.

The manifest will probably launch the processing of your data and determine if a backup should be pushed.

At some point in the future, the backup manager then calls your backup agent's onBackup() method

Restoring a backup is not manual:

Typically you shouldn't ever have to manually request a restore, as it happens automatically when your application is installed on a device. However, if it is necessary to trigger a manual restore, just call the requestRestore() method.

查看更多
Evening l夕情丶
3楼-- · 2019-04-06 18:23

Sigh, the documentation should not be saying "sync" anywhere. I should submit some patches to fix that. :) This mechanism is not intended to provide data exchange between two devices.

The main goal of the backup infrastructure in Android is to give your users a good experience when they set up a new device, such as when they buy a tablet to go with their phone, or when a device gets dropped and broken and they set up a replacement. The idea is that the newly-set-up device be in a familiar state, with all the same apps and their data intact, the same wallpaper imagery when applicable, etc.

Keeping data in sync in realtime between devices is what the Sync Manager facilities are for.

查看更多
登录 后发表回答