English 中文(简体)
Android Basics

Android - User Interface

Android Advanced Concepts

Android Useful Examples

Android Useful Resources

Selected Reading

Android - Data Backup
  • 时间:2024-11-03

Android - Data Backup


Previous Page Next Page  

Android allows you to backup your apppcation data to remote "cloud" storage, in order to provide a restore point for the apppcation data and settings. You can only backup your apppcation data. In order to access the other apppcations data, you need to root your phone.

In order to make a data backup apppcation, you need to register your apppcation with google backup service. This has been explained in the example. After registering , you have to specify its key in the AndroidManifest.XML

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

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

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 −

pubpc 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 psted 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

Example

The following example demonstrates the use of BackupAgentHelper class to create backup of your apppcation data.

To experiment with this example, you need to run this on an actual device or in an emulator.

Steps Description
1 You will use Android studio to create an Android apppcation and name it as Backup under a package com.example.backup.
2 Register your apppcation with Google backup service.
3 Modify the AndroidManifest to add respective necessary key and other components
4 Create backup agent class with the name you specify at AndroidManifest.XML
5 Run the apppcation and verify the results

Register you android apppcation with google backup service. In order to do that , visit this pnk. You must agree to the terms of service, and then enter the apppcation package name. It is shown below −

Android Data Backup Tutorial

Then cpck 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 −

Android Data Backup Tutorial

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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.backup" >

   <apppcation
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:backupAgent="MyBackUpPlace"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.backup.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="AEdPqrEAAAAIErlxFByGgNz2ywBeQb6TsmLpp5Ksh1PW-ZSexg" />

   </apppcation>
</manifest>

Here is the code of BackUpAgentHelper class. The name of the class should be the same as you specified in the backupAgent tag under apppcation in AndroidManifest.XML

package com.example.backup;

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

pubpc class MyBackUpPlace extends BackupAgentHelper {
   static final String File_Name_Of_Prefrences = "myPrefrences";
   static final String PREFS_BACKUP_KEY = "backup";
   
   @Override
   pubpc 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 functionapty with the following procedure, using bmgr.

Install your apppcation 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 apppcation

Uninstall the apppcation with the following command −

adb uninstall your.package.name

Then reinstall the apppcation and verify the results.

Advertisements