android backup功能介绍

本文介绍了Android系统的备份与恢复功能,包括备份的数据类型、存储位置选择及如何通过ADB命令进行测试。适用于开发者了解如何实现应用数据的备份和恢复。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应用:http://hi.baidu.com/sagemlan/item/f5c78709bf8d75db72e67675

2011-07-21 20:34

原创:android backup功能介绍

1. 备份的数据 包括应用程序的一些数据, settings 的一些状态, wifi密码等等。
2. 保存的目的地可以是本地存储空间, 也可以是网络。
3. 当你重新安装实现backup 功能的应用程序时,或者你手机丢失的时候, 你可以restore 之前backup的数据,而不用重新设置就回到你自己喜欢的手机使用状态。
4. 不是所有的应用程序都可以backup, 只有实现了 Backup Agent 的应用程序才具备此功能。 具体实现参考文档。
5. 默认备份到本地, 定义在xml中, 服务器的话要给相应的app添加service key,  minifest.xml中

1. Introduction
Menu option: Settings/Privacy/Back up my data

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.

2. What data is backed up?
You can backup your application data, such as  WIFI passwords, bookmarks of browser, Volume setting, airplane state, BT state and  other settings.

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.

3. Where to store?
    The Data backup should give you another options to store on local SD Card or Online cloud storage.  If you want to store it on cloud, you should first add an account for it.
    Now, on our tequila product, the backup data will be stored on local storage by default. The data for application can be located and viewed  by adb shell command on the following path. Take browser application for example, the file path is
“cache/backup/com.android.browser/”. You can find the data for browser on the file.

    Now, there is no UI option to set where to store the data, and the default one is defined in the xml file.

//frameworks/base/packages/SettingsProvider/default.xml
    <string name="def_backup_transport" translatable="false">android/com.android.internal.backup.LocalTransport</string>  -->local
    <string name="def_backup_transport" translatable="false">com.google.android.backup/.BackupTransportService</string>   -->cloud

  However, you can select the transport by adb shell command:
  Set to local:
  adb shell bmgr transport android/com.android.internal.backup.LocalTransport
  Set to cloud:
  adb shell bmgr transport com.google.android.backup/.BackupTransportService

4. What should be done for a backup & restore application?
  Declaring the Backup Agent in Your Manifest file.
  Registering for Android Backup Service key.
  Extending BackupAgent, Required Methods onBackup() & onRestore().
  Requesting Backup
  Requesting Restore
 

5. How  backup & restore process has been tested?
  Once you've implemented your backup agent, you can test the backup and restore functionality with the following procedure, using bmgr.
  We take com.android.browser package for example.
   1) On local
      a) Make a new bookmark "Baidu" for "http://baidu.com" .
      b) run adb shell command
         adb shell bmgr backup  com.android.browser
         adb shell bmgr run
         If sucessful, you can view the new bookmark you added on “cache/backup/com.android.browser/X2Jvb2ttYXJrc18=”
      such as following:
     =http://www.google.com/m?client=ms-unknown&source=android-home Google http://www.facebook.comFacebook http://www.wikipedia.org/ Wikipedia http://www.ebay.com/ eBay http://www.nytimes.comNY Times9http://picasaweb.google.com/m/viewer?source=androidclient Picasa http://espn.com/ ESPN http://www.amazon.com/ Amazon http://www.weather.com/Weather Channel http://www.bbc.co.uk/ BBC http://www.yahoo.com/Yahoo http://www.msn.com/ MSN http://www.myspace.com/MySpace http://baidu.com/ 0�N�� 0����Baidu

      c) Delete the bookmark "Baidu" you added, then run the following adb command to restore.
          adb shell bmgr restore  com.android.browser
      d) Check the the bookmark, if successful, the bookmark "Baidu" will be restored and display again.

   2) On cloud
      a) Modify the default backup transprot for cloud or modify the Database of settings.db.
         backup_transport:    com.google.android.backup/.BackupTransportService

      b) Registering for Android Backup Service. Modify the manifest file for browser, the service key can be got from
         http://code.google.com/android/backup/index.html
          <meta-data android:name="com.google.android.backup.api_key"
                    android:value="AEdPqrEAAAAIcAjp6zRLRNMRQO4y290PxLiwDk2j67eMJM1K1g" />
      c) make and generate a new sw then flash.
      d) Add a new account, the data for backup and restore is dependent of the account.
      e) Test it as the similar steps on Local above, but you can not view the data stored on cloud.


### Android BackupManager 使用指南:实现数据备份与恢复 Android 提供了 `BackupManager` 机制,允许开发者将应用的用户数据安全地备份到云端,并在需要时恢复这些数据。这种机制基于键值对存储(Key/Value Backup),适用于小型数据集(如设置、偏好或状态信息)。 #### 启用备份功能 要在应用中启用备份功能,首先需要在清单文件 `AndroidManifest.xml` 中声明 `android:allowBackup` 属性: ```xml <application android:allowBackup="true" android:backupAgent=".MyBackupAgent"> </application> ``` 其中 `android:allowBackup="true"` 表示允许应用参与备份和恢复流程,而 `android:backupAgent` 指定了自定义的 `BackupAgent` 子类。 #### 实现 BackupAgent 为了控制备份和恢复逻辑,开发者需要创建一个继承自 `BackupAgent` 的类,并重写其关键方法: - `onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)` - `onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)` 以下是一个简单的实现示例: ```java public class MyBackupAgent extends BackupAgent { private static final String KEY_USER_SETTINGS = "user_settings"; @Override public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException { // 获取当前应用的状态(例如 SharedPreferences 数据) SharedPreferences preferences = getSharedPreferences("app_prefs", Context.MODE_PRIVATE); String settings = preferences.getString("settings_key", ""); // 将数据打包并发送给备份管理器 byte[] buffer = settings.getBytes(); data.writeEntityHeader(KEY_USER_SETTINGS, buffer.length); data.writeEntityData(buffer, buffer.length); } @Override public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException { // 从备份中读取数据 while (data.readNextHeader()) { String key = data.getKey(); int dataSize = data.getDataSize(); if (KEY_USER_SETTINGS.equals(key)) { byte[] buffer = new byte[dataSize]; data.readEntityData(buffer, 0, dataSize); String restoredSettings = new String(buffer); // 将恢复的数据保存到 SharedPreferences SharedPreferences preferences = getSharedPreferences("app_prefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("settings_key", restoredSettings); editor.apply(); } } } } ``` #### 触发备份操作 开发者可以通过调用 `BackupManager.dataChanged()` 方法通知系统某个应用的数据发生了变化,从而触发一次备份请求: ```java BackupManager backupManager = new BackupManager(context); backupManager.dataChanged(); ``` 需要注意的是,调用此方法并不会立即执行备份操作,而是由系统决定何时进行批量处理 [^1]。 #### 备份数据的生命周期 当应用首次安装或从未被备份过时,`oldState` 参数为 `null`。而在后续备份中,系统会传递前一次备份的状态描述符。通过比较新旧状态,可以优化备份效率,避免重复上传相同数据。 #### 恢复操作 恢复通常发生在设备更换、应用重新安装等场景下。当系统检测到存在可用的备份数据时,会自动调用 `onRestore()` 方法。在此过程中,应用应解析传入的 `BackupDataInput` 并更新本地存储以反映云端内容 [^1]。 #### 第三方工具替代方案 对于非开发者的普通用户来说,使用专业工具如 **Coolmuster Android Backup Manager** 可以更方便地完成整个备份和恢复过程。这类软件支持一键式操作,能够选择性地备份特定应用程序及其关联数据至 PC 端,同时提供直观界面简化管理流程 [^2]。 此外,如果希望获得更加全面且灵活的数据保护策略,则推荐采用集成更多高级特性的解决方案,例如 **Coolmuster Android Assistant**。它不仅涵盖了基础级别的文件传输服务,还增强了对多媒体资源以及联系人信息等方面的精细化控制能力 [^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值