<p >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.</p>
<p >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</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="tag" ><application</span><span class="pln" >
</span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:backupAgent</span><span class="pun" >=</span><span class="atv" >"MyBackupPlace"</span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><meta-data</span><span class="pln" >
</span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"com.google.android.backup.api_key"</span><span class="pln" >
</span><span class="atn" >android:value</span><span class="pun" >=</span><span class="atv" >"AEdPqrEAAAAIErlxFByGgNz2ywBeQb6TsmLpp5Ksh1PW-ZSexg"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></application></span></pre>
<p >Android provides<b >BackUpAgentHelper</b>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:</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MyBackUpPlace</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >BackupAgentHelper</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="pun" >}</span></pre>
<p >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<b >SharedPreferencesBackupHelper</b>and<b >FileBackupHelper</b>.</p>
<p >In order to use<b >SharedPerefernceBackupHelper</b>, you need to instantiate its object with the name of your sharedPerefernces File. Its syntax is given below −</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > </span><span class="typ" >File_Name_Of_Prefrences</span><span class="pln" > </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"myPrefrences"</span><span class="pun" >;</span><span class="pln" >
</span><span class="typ" >SharedPreferencesBackupHelper</span><span class="pln" > helper </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >SharedPreferencesBackupHelper</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >File_Name_Of_Prefrences</span><span class="pun" >);</span></pre>
<p >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 −</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pln" >addHelper</span><span class="pun" >(</span><span class="pln" >PREFS_BACKUP_KEY</span><span class="pun" >,</span><span class="pln" > helper</span><span class="pun" >);</span></pre>
<p >The addHelper method will automatically add a helper to a given data subset to the agent's configuration.</p>
<p >Apart from these methods, there are other methods defined in the BackupAgentHelper class. They are defined below −</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th >Sr.No</th>
<th >Method & description</th>
</tr>
<tr >
<td >1</td>
<td ><b >onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)</b>
<p >Run the backup process on each of the configured handlers</p>
</td>
</tr>
<tr >
<td >2</td>
<td ><b >onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)</b>
<p >Run the restore process on each of the configured handlers</p>
</td>
</tr>
</tbody>
</table>
<p >The methods of the SharedPreferencesBackUpHelper class are listed below.</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th >Sr.No</th>
<th >Method & description</th>
</tr>
<tr >
<td >1</td>
<td ><b >performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)</b>
<p >Backs up the configured SharedPreferences groups</p>
</td>
</tr>
<tr >
<td >2</td>
<td ><b >restoreEntity(BackupDataInputStream data)</b>
<p >Restores one entity from the restore data stream to its proper shared preferences file store</p>
</td>
</tr>
</tbody>
</table>
<h2 >Example</h2>
<p >The following example demonstrates the use of BackupAgentHelper class to create backup of your application data.</p>
<p >To experiment with this example, you need to run this on an actual device or in an emulator.</p>
<table class="table table-bordered" >
<tbody >
<tr >
<th >Steps</th>
<th >Description</th>
</tr>
<tr >
<td >1</td>
<td >You will use Android studio 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.</td>
</tr>
<tr >
<td >2</td>
<td >Register your application with Google backup service.</td>
</tr>
<tr >
<td >3</td>
<td >Modify the AndroidManifest to add respective necessary key and other components</td>
</tr>
<tr >
<td >4</td>
<td >Create backup agent class with the name you specify at AndroidManifest.XML</td>
</tr>
<tr >
<td >5</td>
<td >Run the application and verify the results</td>
</tr>
</tbody>
</table>
<p >Register you android application with google backup service. In order to do that ,<a href="https://developer.android.com/google/backup/signup.html" >visit this link</a>. You must agree to the terms of service, and then enter the application package name. It is shown below −</p>
<p><img alt="Android Data Backup Tutorial" src="http://www.tutorialspoint.com/android/images/android_backup1.jpg" /></p>
<p >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 −</p>
<p><img alt="Android Data Backup Tutorial" src="http://www.tutorialspoint.com/android/images/android_backup2.jpg" /></p>
<p >Once you copy the key , you need to write it in your AndroidManifest.XML file. Its code is given below −</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pun" ><?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >"1.0"</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >"utf-8"</span><span class="pun" >?></span><span class="pln" >
</span><span class="tag" ><manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/apk/res/android"</span><span class="pln" >
</span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >"com.example.backup"</span><span class="pln" >
</span><span class="atn" >android:versionCode</span><span class="pun" >=</span><span class="atv" >"1"</span><span class="pln" >
</span><span class="atn" >android:versionName</span><span class="pun" >=</span><span class="atv" >"1.0"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><uses-sdk</span><span class="pln" >
</span><span class="atn" >android:minSdkVersion</span><span class="pun" >=</span><span class="atv" >"8"</span><span class="pln" >
</span><span class="atn" >android:targetSdkVersion</span><span class="pun" >=</span><span class="atv" >"17"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><application</span><span class="pln" >
</span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >"@drawable/ic_launcher"</span><span class="pln" >
</span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >"@string/app_name"</span><span class="pln" >
</span><span class="atn" >android:backupAgent</span><span class="pun" >=</span><span class="atv" >"MyBackUpPlace"</span><span class="pln" >
</span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >"@style/AppTheme"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><activity</span><span class="pln" >
</span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"com.example.backup.MainActivity"</span><span class="pln" >
</span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >"@string/app_name"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><intent-filter></span><span class="pln" >
</span><span class="tag" ><action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.intent.action.MAIN"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.intent.category.LAUNCHER"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></intent-filter></span><span class="pln" >
</span><span class="tag" ></activity></span><span class="pln" >
</span><span class="tag" ><meta-data</span><span class="pln" >
</span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"com.google.android.backup.api_key"</span><span class="pln" >
</span><span class="atn" >android:value</span><span class="pun" >=</span><span class="atv" >"AEdPqrEAAAAIErlxFByGgNz2ywBeQb6TsmLpp5Ksh1PW-ZSexg"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></application></span><span class="pln" >
</span><span class="tag" ></manifest></span></pre>
<p >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</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="pln" >backup</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="pln" >backup</span><span class="pun" >.</span><span class="typ" >BackupAgentHelper</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="pln" >backup</span><span class="pun" >.</span><span class="typ" >SharedPreferencesBackupHelper</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MyBackUpPlace</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >BackupAgentHelper</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > </span><span class="typ" >File_Name_Of_Prefrences</span><span class="pln" > </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"myPrefrences"</span><span class="pun" >;</span><span class="pln" >
</span><span class="kwd" >static</span><span class="pln" > </span><span class="kwd" >final</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > PREFS_BACKUP_KEY </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >"backup"</span><span class="pun" >;</span><span class="pln" >
</span><span class="lit" >@Override</span><span class="pln" >
</span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" >
</span><span class="typ" >SharedPreferencesBackupHelper</span><span class="pln" > helper </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >SharedPreferencesBackupHelper</span><span class="pun" >(</span><span class="kwd" >this</span><span class="pun" >,</span><span class="pln" >
</span><span class="typ" >File_Name_Of_Prefrences</span><span class="pun" >);</span><span class="pln" >
addHelper</span><span class="pun" >(</span><span class="pln" >PREFS_BACKUP_KEY</span><span class="pun" >,</span><span class="pln" > helper</span><span class="pun" >);</span><span class="pln" >
</span><span class="pun" >}</span><span class="pln" >
</span><span class="pun" >}</span></pre>
<h2 >Test your BackupAgent</h2>
<p >Once you've implemented your backup agent, you can test the backup and restore functionality with the following procedure, using bmgr.</p>
<h3 >Install your application on a suitable Android system image.</h3>
<p >If using the emulator, create and use an AVD with Android 2.2 (API Level 8).</p>
<p >If using a device, the device must be running Android 2.2 or greater and have Google Play built in.</p>
<h3 >Ensure data backup is enabled</h3>
<p >If using the emulator, you can enable backup with the following command from your SDK tools/ path −</p>
<pre class="result notranslate" >
adb shell bmgr enable true
</pre>
<p >If using a device, open the system Settings, select Privacy, then enable Back up my data and Automatic restore.</p>
<h3 >Performing backup</h3>
<p >For testing purposes, you can also make a request with the following bmgr command −</p>
<pre class="result notranslate" >
adb shell bmgr backup your.package.name
</pre>
<p >Initiate a backup operation by typing the following command.</p>
<pre class="result notranslate" >
adb shell bmgr run
</pre>
<p >This forces the Backup Manager to perform all backup requests that are in its queue.</p>
<h3 >Uninstall and reinstall your application</h3>
<p >Uninstall the application with the following command −</p>
<pre class="result notranslate" >
adb uninstall your.package.name
</pre>
<p >Then reinstall the application and verify the results.</p>