Implementing cloud data backup based on Android Backup Service

In this article, I would like to touch upon such an important topic for Android developers as cloud data backup using the example of Android Backup Service. This service allows you to store application data for free on remote cloud storage. According to the documentation , it allows you to restore data when reinstalling the application, updating the device and during factory reset. All that is required of the user is that he should have a checkmark in "Data backup" and "Auto restore" in Settings / Restore and reset.


Before implementation, you must register your application directly in the Android Backup Service . The key obtained during registration is used in the manifest, see below.

The easiest way to interact with BackupManager is to inherit from the BackupAgentHelper class.
In this class we use two helpers:
  1. SharedPreferencesBackupHelper for backup SharedPreferences
  2. FileBackupHelper for backup files from internal storage.

public class TheBackupAgent extends BackupAgentHelper {
    public void onCreate() {
            // DatabaseHelper.DATABASE_NAME – константа с названием базы данных. 
            FileBackupHelper fileBackupHelper = new FileBackupHelper(this, "../databases/" + DatabaseHelper.DATABASE_NAME);
            addHelper(DatabaseHelper.DATABASE_NAME, fileBackupHelper);
            //здесь мы просто, через запятую указываем названия файлов, в которых хранятся ваши настройки. Обратите внимание на последний параметр, в файле под таким названием хранит данные дефолтный PreferenceManager(PreferenceManager.getDefaultSharedPreferences(context))
            SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, SharedPreferenceHelper.PREFERENCES, getPackageName() + "_preferences");
            addHelper("prefs", helper);
     //метод для запроса бэкапа. Согласно документации следует вызывать этот метод всякий раз, когда данные изменились. 
     public static void requestBackup(Context context) {
            BackupManager bm = new BackupManager(context);

You will be surprised, but in principle this is all. In the end, we simply indicate the path to the backup agent class in the manifest and additional attributes with a talking name. Also in meta-data the key obtained during registration of the application is registered.

Since Android does not have a beautiful solution for determining the stop of an application, I propose, as one of the lazy ways, to request a backup at the start of the application.

public class MyApplication extends Application {
    public void onCreate() {


Do not forget to carefully look at the logs. If you specify incorrect paths to files or prefs, you will be informed.
The algorithm is quite simple:
  1. We enter data / settings into the application.
  2. We request a backup for the adb shell bmgr backup application
  3. Run the backup manager adb shell bmgr run.
  4. Delete, reinstall, PROFIT.


According to personal feelings and googling, this service does not work stably on all devices, however, the implementation of this functionality is almost effortless and is therefore recommended for use in applications with a database or SharedPreferences by default.

Also popular now: