【发布时间】: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