我已经按照Data Backup所述实现了BackupAgent,注册了一个API key 并在 list 中声明了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();
    }

}

为了完整起见,相关的 list 部分:
<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无法还原此软件包?

最佳答案

尝试在备份类名称之前添加一个点,如下所示:

android:backupAgent=".MyBackupAgent"

或者,如果您的备份类不在根包中,请向其添加路径:
android:backupAgent=".package.MyBackupAgent"

10-04 17:36