BackupAgent subclass used for automatic restore but not manual restore
I've set up the Android Backup Service in my app using a custom class that extends BackupAgentHelper ... it basically looks like this:
public class MyBackups extends BackupAgentHelper {
@Override
public void onCreate() {
Log.d("MyBackups", "creating backup class");
this.addDefaultHelper();
String defaultSharedPrefsName = this.getPackageName() + "_preferences";
SharedPreferencesBackupHelper defaultPrefsHelper = new SharedPreferencesBackupHelper(this, defaultSharedPrefsName);
this.addHelper("default_prefs", defaultPrefsHelper);
}
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) throws IOException {
Log.d("MyBackups", "backing up " + data);
super.onBackup(oldState, data, newState);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState) throws IOException {
Log.d("MyBackups", "restoring");
super.onRestore(data, appVersionCode, newState);
// post-processing code goes here
}
}
I have this registered in the manifest file, and if I delete and reinstall the app, it runs as expected, with all the log messages appearing.
However, if I manually request a restore, like this...
BackupManager backupManager = new BackupManager(getApplicationContext());
int error = backupManager.requestRestore(
new RestoreObserver() {
public void restoreStarting(int numPackages) {
Log.d("MyBackups", "restoreStarting");
}
public void restoreFinished(int error) {
Log.d("MyBackups", "restoreFinished");
}
public void onUpdate(int nowBeingRestored, String currentPackage) {
Log.d("MyBackups", "onUpdate");
}
}
);
Log.d("MyBackups", "requestRestore result: " + error);
...restoreStarting and restoreFinished are called, and the error result is 0, but none of the BackupAgentHelper methods are called -- the "creating backup class" and "restoring" logs don't appear, and my post-processing code doesn't run. It seems as if a manual requestRestore bypasses my custom BackupAgentHelper subclass.
Is there anything else I need to hook up to make a manual restore work the same way as an automatic restore? Have you tried this and is it working for you?
This is an old question, but I was able to do just this today. I hope it helps someone having the same issue.
You need to call BackupManager dataChanged() to send a backup request.
Then, to test it and kick start the backup, you need to run
adb shell bmgr run
This will call OnCreate, OnBackup.
Then after you run backupManager.requestRestore, your OnCreate, OnRestore methods will be called.
Check out the sample here, it does exactly this (when you click the Restore button):
https://android.googlesource.com/platform/development/+/0b3758ea4e53f9bfd0b112eaa4a7dd7b7f4040f5/samples/BackupRestore?autodive=0%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F
See the FileHelperExampleAgent.java agent.
链接地址: http://www.djcxy.com/p/25494.html上一篇: 无法恢复包