Home Tutorials Training Consulting Books Company Contact us


Get more...

Android Data Backup. This tutorial describes the usage of the Android data backup API for restoring Application configuration data.

1. Android Data Backup

1.1. Purpose of data backup

The backup service of Android allows you to copy your persistent application data to a remote cloud storage. This allows you to restore the application data and settings in case the user switches Android devices, re-installs the application or performs a factory reset.

Data backup is not required to be available on all Android devices and the actual cloud storage might be customized by the device manufacturer. Therefore the Android team guarantees no safety for the stored data.

If the service is not available on your device, the backup service is not called, hence it is still save to implement this service and run it on any device.

To use the backup service you have to register your application package for it, via the Backup service registration. This webpage allows you to get a backup key for a application package.

1.2. Backup of shared preferences and files

For saving and restoring your data you extend the BackupAgent class.

The simplest way to implement your backup is to use SharedPreferencesBackupHelper for shared preferences and FileBackupHelper to backup files from the internal storage system. Both helper classes save and restore automatically the registered files.

1.3. General backup

For a more complex backup you can directly implement the BackupAgent and its save and restore methods. See Backup Agenda Documentation for details.

2. Exercise: Implementing a data backup of application settings

2.1. Project implementation

The following application uses the com.vogella.android.databackup top level package. Register this application for the backup service via https://developer.android.com/google/backup/signup.html This package was registered with the Google service.

The backup agenda and the registration key is entered in the AndroidManifest.xml file as in the following listing.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vogella.android.databackup"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:backupAgent="MyBackupAgent"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.vogella.android.databackup.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

</manifest>

The corresponding backup agent can be implemented as demonstrated in the following listing.

package com.vogella.android.databackup;

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

public class MyBackupAgent extends BackupAgentHelper {
    
    // The name of the SharedPreferences file
    static final String PREFS = "myprefs";

    // A key to uniquely identify the set of backup data
    static final String PREFS_BACKUP_KEY = "myprefs";

    @Override
    public void onCreate() {
        SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
        addHelper(PREFS_BACKUP_KEY, helper);
    }

}

2.2. Trigger backup and restore

The implemented backup manager works automatically without user interaction and saves and restores the registered shared preferences associated with the user. Simply change and save shared preferences under the file name which you registered your application with.

If you want to test the save and restore operation via the adb shell command you can use the following commands.

// Trigger a backup, usage:
// adb shell bmgr backup <package>

// schedule backup
adb shell bmgr backup com.vogella.android.databackup
// ensure scheduled backup run
adb shell bmgr run

// to restore you backup use bmgr restore
adb shell bmgr restore com.vogella.android.databackup

3. Android Animation Resources