Android auto backup - get last backup time

328 views Asked by At

I am using the Android AutoBackup feature in my app. These are my manifest settings.

<application
    android:allowBackup="true"
    android:backupAgent=".BackupAgentCustom"
    android:fullBackupContent="@xml/backup_rules"
    android:fullBackupOnly="true"

I implemented a custom BackupAgent as you can see from my manifest settings. I try to save the system time in sharedPreferences when onFullBackup gets called, so i know when the last Backup happend. See this piece of code from my BackupAgent.

 @Override
public void onFullBackup(FullBackupDataOutput data) throws IOException {
    Log.d(TAG, "onFullBackup !!");

    mSharedPreferences.edit().putLong(PREF_KEY_LAST_BACKUP_TIME, System.currentTimeMillis()).apply();

    super.onFullBackup(data);
}

If i start the backup process from commandline or the android backup settings on the device, everything works as expected. Means, i can get the correct LastBackupTime from sharedPreferences.

But on my physical device, its not working. I installed the app with release key on my physical device, waited till next day when the backup process got started from the system. The backup time in the android system settings, and the value in shared preferences is not the same. Seems like the value in sharedPreferences gets not updated.

screenshot from android system settings

My question. How can i get the last backup time for my app?

1

There are 1 answers

1
Gabe Sechan On

Use commit() not apply(). Apply writes to the disk asynchronously. Commit is immediate. You need it to be written immediately. If another section of your app needed the data, apply would be good enough because it writes the data to the in memory shared preferences first. But backup is done by a separate application, so it won't have access to the in memory data.