articles

Home / DeveloperSection / Articles / Android Data Backup

Android Data Backup

Manoj Pandey3535 09-Mar-2015

Android's backup service allows you to copy your persistent application data to remote "cloud" storage, in order to provide a restore point for the application data and settings. If a user performs a factory reset or converts to a new Android-powered device, the system automatically restores your backup data when the application is re-installed. This way, your users don't need to reproduce their previous data or application settings. This process is completely transparent to the user and does not affect the functionality or user experience in your application.

Note-: The backup service is not designed for synchronizing application data with other clients or saving data that you'd like to access during the normal application lifecycle. You cannot read or write backup data on demand and cannot access it in any way other than through the APIs provided by the Backup Manager.

Here is an example of Android Data Backup.

1.      Create an android project with minimum sdk must be greater than 8.

2.      Register for your Android Back Service from this link. 

Android Data Backup 


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: 

Your key is-:

Android Data Backup 

3.      Add key value in your manifest.xml file

<application

   android:allowBackup="true"

   android:backupAgent="MyBackUpActivity">

 

   <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. My BackUpActivity class is

publicclassMyBackUpActivityextendsBackupAgentHelper{
 
}

 

4.      Add following code in AndroidManifest.xml file 

<?xml version="1.0" encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.androidbackupexample"
   android:versionCode="1"
   android:versionName="1.0">
 
   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="18"/>
 
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:backupAgent="com.example.androidbackupexample.MyBackUpActivity"
      android:theme="@style/AppTheme">
      <activity
         android:name="com.example.backup.MainActivity"
         android:label="@string/app_name">
         <intent-filter>
            <actionandroid:name="android.intent.action.MAIN"/>
 
            <categoryandroid:name="android.intent.category.LAUNCHER"/>
         </intent-filter>
      </activity>
      <meta-data
         android:name="com.google.android.backup.api_key"
         android:value="AEdPqrEAAAAIErlxFByGgNz2ywBeQb6TsmLpp5Ksh1PW-ZSexg"/>
 
   </application>
 
</manifest>

 

5.      Paste following code in MyBackUpActivity.class 

package com.example.androidbackupexample; 
import android.app.backup.BackupAgentHelper;
import android.app.backup.SharedPreferencesBackupHelper;
 
public class MyBackUpActivity extends BackupAgentHelper {
                static final String File_Name_Of_Prefrences = "myPrefrences";
                static final String PREFS_BACKUP_KEY = "backup";
 
                @Override
                public void onCreate() {
        // A helper class hat can be used in conjunction with BackupAgentHelper
   //to manage the backup of SharedPreferences.
                                SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(
                  this,File_Name_Of_Prefrences);
                                addHelper(PREFS_BACKUP_KEY,helper);
                }
 
}

 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By