Data backup in android

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

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

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

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 of SharedPreferencesBackupHelper 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.No Method & description
1

onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)

Run the backup process on each of the configured handlers

2

onRestore(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.No Method & description
1

performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)

Backs up the configured SharedPreferences groups

2

restoreEntity(BackupDataInputStream data)

Restores one entity from the restore data stream to its proper shared preferences file store