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.
Manifest has the following under application:
android:allowBackup="true"
android:backupAgent="myBackupHelper"
android:restoreAnyVersion="true"
and metadata
<meta-data
android:name="com.google.android.backup.api_key"
android:value="<my-api-key>" />
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);
}
}
I initialize the BackupManager in the mainactivity as follows:
BackupManager bm = new BackupManager(getApplicationContext());
and call bm.dataChanged();
when the database changes.
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.
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!
The reason why my backupAgentHelper functions were never called is because of the transport being used. Doing
./adb shell bmgr list transports
showed
android/com.android.internal.backup.LocalTransport
*com.google.android.gms/.backup.BackupTransportService
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.
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