Android BackupAgent从未调用过

我正在尝试实现一个自定义backupAgentHelper来备份我的应用程序的数据库文件。 我已经通过了文档几次,但每当我试图强行备份时, backupAgentHelperCreate/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,所以我不知道为什么它没有被击中!


我的backupAgentHelper函数从未被调用的原因是因为使用了传输。 干

./adb shell bmgr list transports

显示

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

出于某种原因,小运输服务不起作用,但将其更改为内部本地运输

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

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

链接地址: http://www.djcxy.com/p/25491.html

上一篇: Android BackupAgent never called

下一篇: Android Studio Rename Package