Android BackupAgent从未调用过

时间:2015-04-13 22:11:51

标签: android android-backup-service

我正在尝试实现自定义backupAgentHelper来备份我的应用的数据库文件。我已多次浏览文档,但每当我尝试强制备份时,backupAgentHelper上的Create/onBackup/onRestore都不会被调用。

Manifest在申请中有以下内容:

android:allowBackup="true"
android:backupAgent="myBackupHelper"
android:restoreAnyVersion="true"

和元数据

    <meta-data 
        android:name="com.google.android.backup.api_key"
        android:value="<my-api-key>" />

myBackupHelper:

public class myBackupHelper extends BackupAgentHelper{

public static String DATABASE_NAME = "db.dat";

@Override
public void onCreate(){
    log.d("Backup oncreate called");
    FileBackupHelper hosts = new FileBackupHelper(this, this.getExternalFilesDir(DATABASE_NAME).getAbsolutePath());
    addHelper(DATABASE_NAME,hosts);
}

@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) {
    log.d("backup onbackup called");
    try {
        //class is the lock since we are using static synchronized methods to read/write
        synchronized (DBManager.class) {
            super.onBackup(oldState, data, newState);
            log.d("Backedup");
        }
    } catch (IOException e) {
        log.d("Backup error, Unable to write to file: " + e);
    }
}

@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState){
    log.d("Backup onrestore called");
    try {
        //class is the lock since we are using static synchronized methods to read/write
        synchronized (DBManager.class) {
            super.onRestore(data, appVersionCode, newState);
        }
    } catch (IOException e) {
        log.d("Backup error, Unable to read from file: " + e);
    }
}

我在mainactivity中初始化BackupManager,如下所示:

BackupManager bm = new BackupManager(getApplicationContext());

并在数据库更改时调用bm.dataChanged();

在测试中,我使用adb强制备份:

./adb shell bmgr backup com.test.android.backuptest
./adb shell bmgr run

但日志永远不会被点击,当我重新安装时,数据永远不会恢复。

注意:备份和恢复设置已启用且设备已超过所需的api 8,因此我不知道为什么它没有被点击!

1 个答案:

答案 0 :(得分:7)

我的backupAgentHelper函数从未被调用的原因是因为正在使用传输。 做

./adb shell bmgr list transports

显示

android/com.android.internal.backup.LocalTransport
*com.google.android.gms/.backup.BackupTransportService

出于某种原因,goodle transportservice无法运行,但是将其更改为内部localtransport

./adb shell bmgr transport android/com.android.internal.backup.LocalTransport

解决了我的问题,现在日志正在显示。