问题描述
我想实现一个自定义的 backupAgentHelper
备份我的应用程序的数据库文件。我已经通过了文档几次走了,但每当我试图强行备份时, backupAgentHelper
在创建/ onBackup / OnRestore中
永远不会被调用。
I am trying to implement a custom backupAgentHelper
to backup my app's database file. I've gone through the docs several times but whenever I try to force backup, the backupAgentHelper
on Create/onBackup/onRestore
are never called.
清单具有以下所申请:
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:
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);
}
}
我初始化BackupManager在MainActivity如下:
I initialize the BackupManager in the mainactivity as follows:
BackupManager bm = new BackupManager(getApplicationContext());
和呼叫 bm.dataChanged();
时,对数据库的修改
and call bm.dataChanged();
when the database changes.
在测试中,我使用adb强制备份:
In testing, I use adb to force backup:
./adb shell bmgr backup com.test.android.backuptest
./adb shell bmgr run
但日志从不打,当我重新安装,数据永远不会恢复。
but the logs are never hit and when i reinstall, data is never restored.
注意:备份和恢复设置启用,该设备拥有所需的API 8,所以我不知道为什么它不被击中
Note: backup and restore settings are enabled and the device has over the required api 8 so I have no idea why its not being hit!
推荐答案
为什么我的backupAgentHelper职能从来没有所谓的原因是因为所使用的交通工具。
做
The reason why my backupAgentHelper functions were never called is because of the transport being used.Doing
./adb shell bmgr list transports
显示
android/com.android.internal.backup.LocalTransport
*com.google.android.gms/.backup.BackupTransportService
由于某些原因,goodle transportservice没有工作,但不断变化的内部localtransport
For some reason the goodle transportservice wasn't working but changing it to the internal localtransport with
./adb shell bmgr transport android/com.android.internal.backup.LocalTransport
固定我的问题,现在日志显示出来。
fixed my problem and now logs are showing up.
这篇关于Android的BackupAgent从来没有所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!