Android 简明教程

Android - Data Backup

Android 允许您将应用程序数据备份到远程“云”存储,以便为应用程序数据和设置提供还原点。您只能备份自己的应用程序数据。要访问其他应用程序数据,您需要对手机进行 root。

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.

要制作数据备份应用程序,您需要将应用程序注册到 Google Backup 服务。该部分已在本示例中进行了解释。注册后,您必须在 AndroidManifest.XML 中指定其密钥。

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 提供 BackUpAgentHelper 类来处理所有数据备份操作。要使用此类,您必须使用它扩展您的类。其语法如下:

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 {
}

您要备份的持久数据应采用两种形式中的一种。它可以是 SharedPreferences,也可以是 File。Android 分别在 SharedPreferencesBackupHelperFileBackupHelper 类的支持下,支持这两种类型的备份。

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.

要使用 SharedPerefernceBackupHelper ,您需要使用您的 sharedPerefernces 文件的名称实例化其对象。其语法如下:

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);

您需要做的最后一件事是通过指定备份密钥字符串和辅助对象来调用 addHelper 方法。其语法如下:

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);

addHelper 方法会自动将一个辅助对象添加到代理的配置中的给定数据子集。

The addHelper method will automatically add a helper to a given data subset to the agent’s configuration.

除了这些方法外,BackupAgentHelper 类中还定义了其他方法。这些方法如下所示:

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

SharedPreferencesBackUpHelper 类的方法如下所示:

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

Example

以下示例演示如何使用 BackupAgentHelper 类创建应用程序数据的备份。

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.

Steps

Description

1

You will use Android studio to create an Android application and name it as Backup under a package com.example.backup.

2

Register your application 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 application and verify the results

使用 Google 备份服务注册你 Android 应用程序。为此, visit this link 。你必须同意服务条款,然后输入应用程序包名称。如下图所示:

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 −

android backup1

然后,单击“使用 Android 备份服务注册”。它会给你你的密钥以及 AndroidManifest 代码可供复制。只需复制密钥。如下图所示:

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 −

android backup2

一旦你复制了密钥,你就需要在 AndroidManifest.XML 文件中写入它。其代码如下所示:

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" >

   <application
      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" />

   </application>
</manifest>

以下是 BackUpAgentHelper 类的代码。类的名称应与你指定的相同在 AndroidManifest.XML 中 application 中 backupAgent 标签中

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

一旦你实现备份代理,你可以使用以下过程和 bmgr 测试备份和恢复功能。

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.

如果使用模拟器,请创建一个使用 Android 2.2(API 级别 8)的 AVD。

If using the emulator, create and use an AVD with Android 2.2 (API Level 8).

如果使用设备,该设备必须运行 Android 2.2 或更高版本,并且已经内置 Google Play。

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

如果你使用模拟器,你可以使用 SDK 工具/路径执行以下命令以启用备份:

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

出于测试目的,您还可以使用以下 bmgr 命令发出请求−

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.