gpt4 book ai didi

android - 无法备份我的应用程序数据库

转载 作者:太空狗 更新时间:2023-10-29 13:53:06 38 4
gpt4 key购买 nike

我有以下代码:

我的备份地点

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());
}
}

数据库助手

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 得到备份 key .

然后我尝试运行:

adb shell bmgr transport 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 transport 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

我的代码可能有什么问题?

最佳答案

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

Settings > Back up my data

关于android - 无法备份我的应用程序数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43335856/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com