Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

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 at the following URL: https://developer.android.com/google/backup/signup.html - 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 http://developer.android.com/guide/topics/data/backup.html - 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. 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. About this website

4. Android Animation Resources

4.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.