I have implemented the BackupAgent as described at Data Backup, registered an API key and declared the BackupAgent in my Manifest.
The Backup part work's quite well, I think; When I run adb shell bmgr run
in command line, the following output appears in LogCat:
01-11 22:23:09.002: DEBUG/PerformBackupThread(97): starting agent for backup of BackupRequest{app=ApplicationInfo{4547c5b8 com.meins.nightclock} full=false}
01-11 22:23:09.002: DEBUG/BackupManagerService(97): awaiting agent for ApplicationInfo{4547c5b8 com.meins.nightclock}
01-11 22:23:09.013: DEBUG/BackupManagerService(97): agentConnected pkg=com.meins.nightclock agent=android.os.BinderProxy@4536a7f8
01-11 22:23:09.032: DEBUG/BackupHelperDispatcher(9122): handling existing helper 'alarms' android.app.backup.FileBackupHelper@44e197b0
01-11 22:23:09.032: DEBUG/BackupHelperDispatcher(9122): handling existing helper 'prefs' android.app.backup.SharedPreferencesBackupHelper@44e19478
01-11 22:23:09.032: VERBOSE/LocalTransport(97): performBackup() pkg=com.meins.nightclock
01-11 22:23:09.032: VERBOSE/LocalTransport(97): Got change set key=alarms:alarms size=16 key64=YWxhcm1zOmFsYXJtcw==
01-11 22:23:09.042: VERBOSE/LocalTransport(97): data size 16
01-11 22:23:09.062: VERBOSE/LocalTransport(97): Got change set key=prefs:com.meins.nightclock_preferences size=265 key64=cHJlZnM6Y29tLm1laW5zLm5pZ2h0Y2xvY2tfcHJlZmVyZW5jZXM=
01-11 22:23:09.072: VERBOSE/LocalTransport(97): data size 265
01-11 22:23:09.072: VERBOSE/LocalTransport(97): finishBackup()
The restore part on the other hand doesn't work at all, on onReceive()
method is not called. When I reinstall my app, the only output which may (?) refer to the BackupAgent is
01-11 22:14:01.042: DEBUG/vending(7426): [100] LocalAssetCache.updateOnePackage(): No local info for com.meins.nightclock
When I run adb shell bmgr restore com.meins.nightclock
it simply states Unable to restore package com.meins.nightclock
.
I am using the following implementation of BackupAgentHelper
package com.meins.nightclock;
import java.io.IOException;
import android.app.backup.BackupAgentHelper;
import android.app.backup.BackupDataInput;
import android.app.backup.BackupDataOutput;
import android.app.backup.FileBackupHelper;
import android.app.backup.SharedPreferencesBackupHelper;
import android.os.ParcelFileDescriptor;
import android.util.Log;
public class MyBackupAgent extends BackupAgentHelper {
public static final Object[] DATA_LOCK = new Object[0];
private static final String PREFS_BACKUP_KEY = "prefs";
private static final String ALARMS_BACKUP_KEY = "alarms";
private DataLayerAlarms d;
@Override
public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
ParcelFileDescriptor newState) throws IOException {
if (d.backup() != 0)
return;
synchronized (DATA_LOCK) {
super.onBackup(oldState, data, newState);
}
}
@Override
public void onCreate() {
SharedPreferencesBackupHelper preferencesHelper = new SharedPreferencesBackupHelper(this,
getPackageName() + "_preferences");
addHelper(PREFS_BACKUP_KEY, preferencesHelper);
FileBackupHelper fileHelper = new FileBackupHelper(this, DataLayerAlarms.BACKUP_FILE);
addHelper(ALARMS_BACKUP_KEY, fileHelper);
d = new DataLayerAlarms(this);
}
@Override
public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
throws IOException {
Log.d(toString(), "onRestore()");
synchronized (DATA_LOCK) {
super.onRestore(data, appVersionCode, newState);
}
d.restore();
}
}
For the sake of completeness the relevant Manifest part:
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true"
android:backupAgent="MyBackupAgent">
<meta-data
android:name="com.google.android.backup.api_key"
android:value="AEdPqrEAAAAI4MsUOC-[...]" />
...
</application>
Does anyone know, why the BackupManager is unable to restore this package?
Try adding a dot before your backup class name, as in:
android:backupAgent=".MyBackupAgent"
Or if your backup class isn't in your root package, add the path to it:
android:backupAgent=".package.MyBackupAgent"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With