我按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()
方法。当我重新安装我的应用程序时,可能(?)引用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无法恢复此软件包?
答案 0 :(得分:3)
尝试在备份类名称前添加一个点,如:
android:backupAgent=".MyBackupAgent"
或者如果您的备份类不在您的根包中,请添加它的路径:
android:backupAgent=".package.MyBackupAgent"
答案 1 :(得分:0)
将android:backupAgent="MyBackupAgent"
更改为android:backupAgent=".MyBackupAgent"
也适用于我。
答案 2 :(得分:0)
当我更改包名称时出现此问题,然后出现其他问题。我花了几个小时才搞清楚。
禁用即时运行。这给我搞砸了。
检查传输列表,确保已设置Google:
adb shell bmgr list transports
android/com.android.internal.backup.LocalTransport
* com.google.android.gms/.backup.BackupTransportService
确保BackupAgentHelper软件包名称和元数据包名称具有相同的名称。名称冲突可能会导致问题。
android:backupAgent实现的类的名称 应用程序的备份代理,BackupAgent的子类。属性 value应该是一个完全限定的类名(例如, &#34; com.example.project.MyBackupAgent&#34)。但是,作为速记,如果 名称的第一个字符是句点(例如, &#34; .MyBackupAgent&#34;),它附加到指定的包名称中 元件。没有默认值。必须指定名称。