How To: Backup Your Settings with Android Backup Service

Wouldn’t it be nice if you get a new phone, and you install Angry Birds, and you can just continue where you left off?

When a user turns on their Android phone for the first time and goes through the initial setup, they are offered the chance to opt-in to have Google back up their apps and settings to Google’s servers. If they opt in, all of their backed up settings will be restored and previously installed apps will download from the Google Play Store. As a bonus, those apps can also backup their settings to be restored on a new device as well. Of course, the developer has to configure their app to handle this, and not enough do. Anything less than all apps is not enough.

Using the Backup Service is simple enough to do. It takes less than 5 minutes to get your app to use the Backup Service, so you should definitely take the time to use it. Having your settings automatically makes for a great user experience. The user doesn’t have to go through all your settings just to repeat a process they’ve already done.

Requirements

  • API Level 8 – Android 2.2

The way Android Backup Service works is that it backs up your files whenever you request it to do so. When the app gets restored, your files get restored with it. You never have to manually request a restore, but you have the ability to do so, if needed.

To get started, you first need to register with the Backup Service here. Once you do, you’ll get an API key. Save this key, we’ll use it in a bit.

Now create a class that extends `BackupAgentHelper`.

import android.app.backup.BackupAgentHelper;
import android.app.backup.SharedPreferencesBackupHelper;

public class MyBackupAgent extends BackupAgentHelper {
    private static final String MY_PREFERENCES = "MY_PREFERENCES";
    private static final String MY_FILE = "MY_FILE"
    private static final String MY_FILE_BACKUP_KEY = "MY_PREFERENCES_BACKUP_KEY";
    private static final String MY_FILE_BACKUP_KEY = "MY_FILE_BACKUP_KEY";

    public void onCreate() {
        SharedPreferencesBackupHelper preferenceHelper =
                new SharedPreferencesBackupHelper(this, MY_PREFERENCES);
        FileBackupHelper fileHelper =
                new FileBackupHelper(this, MY_FILE);

        addHelper(MY_BACKUP_KEY, preferencesHelper);
        addHelper(MY_BACKUP_KEY, fileHelper);
    }
}

You can backup multiple files by adding them to the initiation of `helper` like this `new FileBackupHelper(this, PREFS_1, PREFS_2, PREFS_3, …)` or `BackupHelper(this, FILE_1, FILE_2, FILE_3, …)`.

Now add your new class to the manifest, as well as your API key from earlier.

<manifest ...
    android:backupAgent="MyBackupAgent" >

    <meta-data android:name="com.google.android.backup.api_key"
    	android:value="..." />

All you have to do now is request a backup of your files whenever your dataset has changed. You can do this anywhere in your application by simply initiating the `BackupManager` and calling the `dataChanged()` method.

    public void requestBackup() {
        BackupManager bm = new BackupManager(this);
        bm.dataChanged();
    }

Ideally, you’ll add this anywhere you edit your files or preferences, including the `onSharedPreferenceChanged()` method of any `PreferenceActivity` you may have.

That’s it! Now when a user restores a device, their settings will return with it. Perfect, huh?

Keep in mind that this is just the simplest implementation of the Backup Service. If you’d like more information on the Android Backup Service, including more complex implementations, check out the Android Developer Guide.

Leave a Reply

Your email address will not be published. Required fields are marked *