Android Data Backup Tutorial - Android Tutorial


Android allows you to backup your application data to remote "cloud" storage, in order to provide a restore point for the application data and settings. You can only backup your application data. In order to access the other applications data, you need to root your phone.
In order to make a data backup application, you need to register your application with google backup service. This has been explained in the example. After registering , you have to specify its key in the AndroidManifest.XML

android:allowBackup="true"
android:backupAgent="MyBackupPlace">


android:name="com.google.android.backup.api_key"
android:value="AEdPqrEAAAAIErlxFByGgNz2ywBeQb6TsmLpp5Ksh1PW-ZSexg" />
Android provides BackUpAgentHelper class to handle all the operations of data backup. In order to use this class , you have to extend your class with it. Its syntax is given below:
public class MyBackUpPlace extends BackupAgentHelper {

}
The persistent data that you want to backup is in either of the two forms. Either it could be SharedPrefrences or it could be File. Android supports both types of backup in the respective classes ofSharedPreferencesBackupHelper and FileBackupHelper.
In order to use SharedPerefernceBackupHelper, you need to instantiate its object with the name of your sharedPerefernces File. Its syntax is given below:
static final String File_Name_Of_Prefrences = "myPrefrences";
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, File_Name_Of_Prefrences);
The last thing you need to do is to call addHelper method by specifying the backup key string , and the helper object. Its syntax is given below:
addHelper(PREFS_BACKUP_KEY, helper);
The addHelper method will automatically add a helper to a given data subset to the agent's configuration.
Apart from these methods, there are other methods defined in the BackupAgentHelper class. They are defined below:
Sr.NoMethod & description
1onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)
Run the backup process on each of the configured handlers
2onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
Run the restore process on each of the configured handlers
The methods of the SharedPreferencesBackUpHelper class are listed below.
Sr.NoMethod & description
1performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)
Backs up the configured SharedPreferences groups
2restoreEntity(BackupDataInputStream data)
Restores one entity from the restore data stream to its proper shared preferences file store

Example

The following example demonstrates the use of BackupAgentHelper class to create backup of your application data.
To experiment with this example , you need to run this on an actual device or in an emulator.
StepsDescription
1You will use Eclipse IDE to create an Android application and name it as Backup under a package com.example.backup. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2Register your application with Google backup service.
3Modify the AndroidManifest to add respective necessary key and other compoenets
4Create backup agent class with the name you specify at AndroidManifest.XML
5Run the application and verify the results
Register you android application with google backup service. In order to do that , visit this link. You must agree to the terms of service, and then enter the application package name. It is shown below:
Then click on Register with android backup service. It would give you your key, along with your AndroidManifest code to copy. Just copy the key. It is shown below:
Once you copy the key , you need to write it in your AndroidManifest.XML file. Its code is given below:
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.backup"
android:versionCode="1"
android:versionName="1.0" >


android:minSdkVersion="8"
android:targetSdkVersion="17" />


android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:backupAgent="MyBackUpPlace"
android:theme="@style/AppTheme" >

android:name="com.example.backup.MainActivity"
android:label="@string/app_name" >

android:name="android.intent.action.MAIN" />

android:name="android.intent.category.LAUNCHER" />
android:name="com.google.android.backup.api_key" android:value="AEdPqrEAAAAIErlxFByGgNz2ywBeQb6TsmLpp5Ksh1PW-ZSexg" />
Here is the code of BackUpAgentHelper class. The name of the class should be the same as you specified in the backupAgent tag under application in AndroidManifest.XML
package com.example.backup;

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

public class MyBackUpPlace extends BackupAgentHelper {


static final String File_Name_Of_Prefrences = "myPrefrences";
static final String PREFS_BACKUP_KEY = "backup";

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

}

Test your BackupAgent

Once you've implemented your backup agent, you can test the backup and restore functionality with the following procedure, using bmgr.

INSTALL YOUR APPLICATION ON A SUITABLE ANDROID SYSTEM IMAGE.

If using the emulator, create and use an AVD with Android 2.2 (API Level 8).
If using a device, the device must be running Android 2.2 or greater and have Google Play built in.

ENSURE DATA BACKUP IS ENABLED

If using the emulator, you can enable backup with the following command from your SDK tools/ path:
adb shell bmgr enable true
If using a device, open the system Settings, select Privacy, then enable Back up my data and Automatic restore.

PERFORMING BACKUP

For testing purposes, you can also make a request with the following bmgr command:
adb shell bmgr backup your.package.name
Initiate a backup operation by typing the following command.
adb shell bmgr run
This forces the Backup Manager to perform all backup requests that are in its queue.

UNINSTALL AND REINSTALL YOUR APPLICATION

Uninstall the application with the following command:
adb uninstall your.package.name
Then reinstall the application and verify the results.