【问题标题】:Unable to backup my app database无法备份我的应用程序数据库
【发布时间】:2017-04-11 02:34:28
【问题描述】:

我有以下代码:

MyBackupPlace

public class MyBackupPlace extends BackupAgentHelper {
    static final String FILES_BACKUP_KEY = "myfiles";

    // Allocate a helper and add it to the backup agent
    @Override
    public void onCreate() {
        DbBackupHelper helper = new DbBackupHelper(this, DBHelper.DATABASE_NAME);
        addHelper(FILES_BACKUP_KEY, helper);
    }

    /**
     * We want to ensure that the UI is not trying to rewrite the data file
     * while we're reading it for backup, so we override this method to
     * supply the necessary locking.
     */
    @Override
    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
                         ParcelFileDescriptor newState) throws IOException {
        System.out.println("onBackup");
        synchronized (MainActivity.sDataLock) {
            super.onBackup(oldState, data, newState);
        }
    }

    /**
     * Adding locking around the file rewrite that happens during restore is
     * similarly straightforward.
     */
    @Override
    public void onRestore(BackupDataInput data, int appVersionCode,
                          ParcelFileDescriptor newState) throws IOException {
        synchronized (MainActivity.sDataLock) {
            System.out.println("onRestore");
            super.onRestore(data, appVersionCode, newState);
        }
    }
}

DBBackupHelper

public class DbBackupHelper extends FileBackupHelper {

    public DbBackupHelper(Context ctx, String dbName) {
        super(ctx, ctx.getDatabasePath(dbName).getAbsolutePath());
    }
}

DBHelper

public class DBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "testDB.db";
    ....
}

AndroidManifest

<activity
    android:name=".MainActivity"
    android:allowBackup="true"
    android:backupAgent="MyBackupPlace"
    android:restoreAnyVersion="true">
    ...    
</activity

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

我从here 获得备份密钥。

然后我尝试运行:

adb shell bmgr 传输 com.google.android.gms/.backup.BackupTransportService

adb shell bmgr backupnow my_package_name

它返回我:

Running backup for 1 requested packages.
Package @pm@ with result: Transport error
Backup finished with result: Transport error

如果我使用其他交通工具:

adb shell bmgr 传输 android/com.android.internal.backup.LocalTransport

adb shell bmgr backupnow my_package_name

它返回我:

Running backup for 1 requested packages.
Package @pm@ with result: Success
Backup finished with result: Backup is not allowed

我的代码有什么问题?

【问题讨论】:

    标签: android android-backup-service


    【解决方案1】:

    我需要在我的设备上启用一些设置:

    设置 > 备份我的数据

    【讨论】:

    • 在我的情况下(小米 Android 10):设置 > 帐户和同步 > 自动同步数据
    【解决方案2】:

    如果您使用的是测试设备,很可能“备份”未启用。例如,这是模拟器“Android系统设置->备份”的默认设置。

    要启用它,您必须使用 Google 帐户登录(因为每个用户的备份数据都存储在 Google 云端硬盘中)。我只是最终用我的个人手机进行了测试。

    【讨论】:

      【解决方案3】:

      我解决了禁用 高保护(加密)的问题,如附图所示:

      【讨论】:

        猜你喜欢
        • 2019-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-18
        • 2013-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多