Android 系列 2.13备份Android应用程序数据

263 篇文章 2 订阅
164 篇文章 0 订阅
2.13备份Android应用程序数据


问题
当用户执行出厂重置或转换为新的Android设备时,应用程序丢失存储的数据或应用程序设置。

Android的备份管理器有助于在重新安装应用程序时自动还原备份数据或应用程序设置。
讨论
Android的备份管理器基本上以两种模式运行,即备份和恢复。
在备份操作期间,备份管理器(BackupManager类)向应用程序查询备份数据,然后将其传送到备份传输,然后将备份传输到基于云的存储。在恢复操作期间,备份管理器从备份传输中检索备份数据,并将其返回到应用程序,以便应用程序可以将数据恢复到设备。您的应用程序可以请求恢复,但不是必需的,因为在安装应用程序并且与用户关联的备份数据存在时,Android会执行恢复操作。备份数据恢复的主要方案发生在用户重置设备或升级到新设备并重新安装先前安装的应用程序时。
示例2-17显示了如何为应用程序实现备份管理器,以便可以保存应用程序的当前状态。

实施例2-17。备份/恢复布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:text="@string/filling_text"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RadioGroup android:id="@+id/filling_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<RadioButton android:id="@+id/bacon"
android:text="@string/bacon_label"/>
<RadioButton android:id="@+id/pastrami"
android:text="@string/pastrami_label"/>
<RadioButton android:id="@+id/hummus"
android:text="@string/hummus_label"/>
</RadioGroup>
<TextView android:text="@string/extras_text"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/mayo"
android:text="@string/mayo_text"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/tomato"
android:text="@string/tomato_text"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

这里是逐步形式的过程的基本描述:
1.在Eclipse中创建一个BackupManagerExample项目。
2.打开并将示例2-17中的代码插入到layout / backup_restore.xml文件中。
3.打开values / string.xml文件,并插入示例2-18中显示的代码。
4.您的清单文件将如示例2-19所示。
5.示例2-20中的代码完成了应用程序的备份管理器的实现。
实施例2-18。 字符串的例子

<resources>
<string name="hello">Hello World, BackupManager!</string>
<string name="app_name">BackupManager</string>
<string name="filling_text">Choose Settings for your application:</string>
<string name="bacon_label">Sound On</string>
<string name="pastrami_label">Vibration On</string>
<string name="hummus_label">Backlight On</string>
<string name="extras_text">Extras:</string>
<string name="mayo_text">Use Orientation?</string>
<string name="tomato_text">Use Camera?</string>
</resources>
例子 2-19. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sym.backupmanager"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:label="Backup/Restore" android:icon="@drawable/icon"
android:backupAgent="ExampleAgent"> <!-- Here you specify the backup agent-->
<!--Some backup transports may require API keys or other metadata-->
<meta-data android:name="com.google.android.backup.api_key"
android:value="INSERT YOUR API KEY HERE" />
<activity android:name=".BackupManagerExample">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </application>
</manifest>
实施例2-20。 备份/恢复活动

package com.sym.backupmanager;
import android.app.Activity;
import android.app.backup.BackupManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class BackupManagerExample extends Activity {
static final String TAG = "BRActivity";
static final Object[] sDataLock = new Object[0];
static final String DATA_FILE_NAME = "saved_data";
RadioGroup mFillingGroup;
CheckBox mAddMayoCheckbox;
CheckBox mAddTomatoCheckbox;
File mDataFile;
BackupManager mBackupManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.backup_restore);
mFillingGroup = (RadioGroup) findViewById(R.id.filling_group);
mAddMayoCheckbox = (CheckBox) findViewById(R.id.mayo);
mAddTomatoCheckbox = (CheckBox) findViewById(R.id.tomato);
mDataFile = new File(getFilesDir(), BackupManagerExample.DATA_FILE_NAME);
mBackupManager = new BackupManager(this);
populateUI();
}
void populateUI() {
RandomAccessFile file;
int whichFilling = R.id.pastrami;
boolean addMayo = false;
boolean addTomato = false;
synchronized (BackupManagerExample.sDataLock) {
boolean exists = mDataFile.exists();
try {
file = new RandomAccessFile(mDataFile, "rw");
if (exists) {
Log.v(TAG, "datafile exists");
whichFilling = file.readInt();
addMayo = file.readBoolean();
addTomato = file.readBoolean();
Log.v(TAG, " mayo=" + addMayo
+ " tomato=" + addTomato
+ " filling=" + whichFilling);
} else {
Log.v(TAG, "creating default datafile");
writeDataToFileLocked(file,
addMayo, addTomato, whichFilling);
mBackupManager.dataChanged();
}
} catch (IOException ioe) {
// Do some error handling here!
}
}
mFillingGroup.check(whichFilling);
mAddMayoCheckbox.setChecked(addMayo);
mAddTomatoCheckbox.setChecked(addTomato);
mFillingGroup.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group,
int checkedId) {
Log.v(TAG, "New radio item selected: " + checkedId);
recordNewUIState();
}
});
CompoundButton.OnCheckedChangeListener checkListener
= new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.v(TAG, "Checkbox toggled: " + buttonView);
recordNewUIState();
}
};
mAddMayoCheckbox.setOnCheckedChangeListener(checkListener);
mAddTomatoCheckbox.setOnCheckedChangeListener(checkListener);
}
void writeDataToFileLocked(RandomAccessFile file, boolean addMayo, boolean addTomato, int whichFilling)
throws IOException {
file.setLength(0L);
file.writeInt(whichFilling);
file.writeBoolean(addMayo);
file.writeBoolean(addTomato);
Log.v(TAG, "NEW STATE: mayo=" + addMayo
+ " tomato=" + addTomato
+ " filling=" + whichFilling);
}
void recordNewUIState() {
boolean addMayo = mAddMayoCheckbox.isChecked();
boolean addTomato = mAddTomatoCheckbox.isChecked();
int whichFilling = mFillingGroup.getCheckedRadioButtonId();
try {
synchronized (BackupManagerExample.sDataLock) {
RandomAccessFile file = new RandomAccessFile(mDataFile, "rw");
writeDataToFileLocked(file, addMayo, addTomato, whichFilling);
}
} catch (IOException e) {
Log.e(TAG, "Unable to record new UI state");
}
mBackupManager.dataChanged();
}
}


数据备份不能保证在所有Android设备上都可用。但是,如果设备不提供备份传输,则您的应用程序不会受到负面影响。如果您认为用户将从应用程序中的数据备份中受益,您可以按照此配方中所述实施,测试它,然后发布应用程序,而不必担心哪些设备实际执行备份。当应用程序在不提供备份传输的设备上运行时,您的应用程序将正常运行,但不会从备份管理器接收到备份数据的回调。
虽然您不知道当前传输是什么,但您始终可以确保设备上的其他应用程序无法读取您的备份数据。只有备份管理器和备份传输可以访问您在备份操作期间提供的数据。
由于云存储和传输服务在设备之间可能不同,因此在使用备份时,Android不能保证您的数据的安全性。您应始终谨慎使用备份来存储敏感数据,例如用户名和密码。
测试您的备份代理
一旦实现了备份代理,就可以使用bmgr命令来测试备份和恢复功能,具体步骤如下:
1.在适当的Android系统映像上安装应用程序,并使用Google Play服务运行任何当前的模拟器或设备。
2.确保启用备份功能。如果您正在使用仿真器,则可以使用SDK工具/路径中的以下命令启用备份:
adb shell bmgr enable true
如果您正在使用设备,请打开系统设置,选择隐私,然后启用“备份我的数据”和“自动还原”。
3.打开应用程序并初始化一些数据。
如果您已在应用程序中正确实现备份功能,则应在每次数据更改时请求备份。例如,每次用户更改某些数据时,您的应用程序应调用dataChanged(),它会向备份管理器队列添加一个备份请求。出于测试目的,您还可以使用以下bmgr命令进行请求:
adb shell bmgr backup your.package.name
4.启动备份操作:
adb shell bmgr run
这将强制备份管理器执行其队列中的所有备份请求。
5.卸载应用程序:
adb uninstall your.package.name
6.重新安装应用程序。
如果备份代理成功,则会还原在步骤4中初始化的所有数据。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值