我已经按照Data Backup中的描述实现了 BackupAgent ,注册了一个 API 密钥并在我的 Manifest 中声明了 BackupAgent。我认为备份部分工作得很好;当我adb shell bmgr run
在命令行中运行时,LogCat 中会出现以下输出:
01-11 22:23:09.002: DEBUG/PerformBackupThread(97): starting agent for backup of BackupRequest{app=ApplicationInfo{4547c5b8 com.meins.nightclock} full=false}
01-11 22:23:09.002: DEBUG/BackupManagerService(97): awaiting agent for ApplicationInfo{4547c5b8 com.meins.nightclock}
01-11 22:23:09.013: DEBUG/BackupManagerService(97): agentConnected pkg=com.meins.nightclock agent=android.os.BinderProxy@4536a7f8
01-11 22:23:09.032: DEBUG/BackupHelperDispatcher(9122): handling existing helper 'alarms' android.app.backup.FileBackupHelper@44e197b0
01-11 22:23:09.032: DEBUG/BackupHelperDispatcher(9122): handling existing helper 'prefs' android.app.backup.SharedPreferencesBackupHelper@44e19478
01-11 22:23:09.032: VERBOSE/LocalTransport(97): performBackup() pkg=com.meins.nightclock
01-11 22:23:09.032: VERBOSE/LocalTransport(97): Got change set key=alarms:alarms size=16 key64=YWxhcm1zOmFsYXJtcw==
01-11 22:23:09.042: VERBOSE/LocalTransport(97): data size 16
01-11 22:23:09.062: VERBOSE/LocalTransport(97): Got change set key=prefs:com.meins.nightclock_preferences size=265 key64=cHJlZnM6Y29tLm1laW5zLm5pZ2h0Y2xvY2tfcHJlZmVyZW5jZXM=
01-11 22:23:09.072: VERBOSE/LocalTransport(97): data size 265
01-11 22:23:09.072: VERBOSE/LocalTransport(97): finishBackup()
另一方面,还原部分根本不起作用,onReceive()
不会调用 on 方法。当我重新安装我的应用程序时,唯一可能(?)引用 BackupAgent 的输出是
01-11 22:14:01.042: DEBUG/vending(7426): [100] LocalAssetCache.updateOnePackage(): No local info for com.meins.nightclock
当我运行它时adb shell bmgr restore com.meins.nightclock
,它只是说明Unable to restore package com.meins.nightclock
.
我正在使用以下实现BackupAgentHelper
package com.meins.nightclock;
import java.io.IOException;
import android.app.backup.BackupAgentHelper;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.app.backup.FileBackupHelper;
import android.app.backup.SharedPreferencesBackupHelper;
import android.os.ParcelFileDescriptor;
import android.util.Log;
public class MyBackupAgent extends BackupAgentHelper {
public static final Object[] DATA_LOCK = new Object[0];
private static final String PREFS_BACKUP_KEY = "prefs";
private static final String ALARMS_BACKUP_KEY = "alarms";
private DataLayerAlarms d;
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
ParcelFileDescriptor newState) throws IOException {
if (d.backup() != 0)
return;
synchronized (DATA_LOCK) {
super.onBackup(oldState, data, newState);
}
}
@Override
public void onCreate() {
SharedPreferencesBackupHelper preferencesHelper = new SharedPreferencesBackupHelper(this,
getPackageName() + "_preferences");
addHelper(PREFS_BACKUP_KEY, preferencesHelper);
FileBackupHelper fileHelper = new FileBackupHelper(this, DataLayerAlarms.BACKUP_FILE);
addHelper(ALARMS_BACKUP_KEY, fileHelper);
d = new DataLayerAlarms(this);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
Log.d(toString(), "onRestore()");
synchronized (DATA_LOCK) {
super.onRestore(data, appVersionCode, newState);
}
d.restore();
}
}
为了完整起见,相关的清单部分:
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true"
android:backupAgent="MyBackupAgent">
<meta-data
android:name="com.google.android.backup.api_key"
android:value="AEdPqrEAAAAI4MsUOC-[...]" />
...
</application>
有谁知道,为什么 BackupManager 无法恢复这个包?