public class BackupDataOutput
extends Object
| java.lang.Object | |
| android.app.backup.BackupDataOutput | |
提供结构化接口, BackupAgent通过其onBackup()方法将信息提交到备份数据集。 为备份写入的数据表示为一组“实体”键/值对,其中每个二进制数据记录“值”用字符串“键”命名。
要向备份传输提交数据记录,代理的方法onBackup()首先会写入一个“实体标题”,用于提供记录的密钥字符串以及该记录的二进制值的总大小。 标题写完后,代理会自己写入二进制实体值。 如果需要,可以将实体值写入多个块中,只要写入的字节总数与提供给writeEntityHeader()的字节数相匹配writeEntityHeader() 。
实体密钥字符串在给定应用程序的备份数据集中被认为是唯一的。 如果备份代理在现有的密钥字符串下写入新的实体,则其值将替换传输的远程数据存储中的任何先前值。 您可以使用现有记录的密钥写入新的实体标题,但是提供一个负数参数dataSize ,从而完全从远程数据集中删除记录。 当您这样做时,代理不需要致电writeEntityData(byte[], int) 。
以下是一个示例,说明如何备份名为 mStringToBackUp的String变量的值:
static final String MY_STRING_KEY = "storedstring";
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)
throws IOException {
...
byte[] stringBytes = mStringToBackUp.getBytes();
data.writeEntityHeader(MY_STRING_KEY, stringBytes.length);
data.writeEntityData(stringBytes, stringBytes.length);
...
}
也可以看看:
公共方法(Public methods) |
|
|---|---|
int |
writeEntityData(byte[] data, int size) 将当前实体下的一大块数据写入备份传输。 |
int |
writeEntityHeader(String key, int dataSize) 在备份数据流中标记一条记录的开始。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
int writeEntityData (byte[] data,
int size)
将当前实体下的一大块数据写入备份传输。
| 参数(Parameters) | |
|---|---|
data |
byte: A raw data buffer to send |
size |
int: The number of bytes to be sent in this chunk |
| 返回(Returns) | |
|---|---|
int |
the number of bytes written |
| 抛出异常(Throws) | |
|---|---|
IOException |
if the write failed |
int writeEntityHeader (String key, int dataSize)
在备份数据流中标记一条记录的开始。 这必须在writeEntityData(byte[], int)之前writeEntityData(byte[], int) 。
| 参数(Parameters) | |
|---|---|
key |
String: A string key that uniquely identifies the data record within the application. Keys whose first character is or higher are not valid. |
dataSize |
int: The size in bytes of this record's data. Passing a dataSize of -1 indicates that the record under this key should be deleted. |
| 返回(Returns) | |
|---|---|
int |
The number of bytes written to the backup stream |
| 抛出异常(Throws) | |
|---|---|
IOException |
if the write failed |