Toni4780
Toni4780

Reputation: 453

Android BackupAgent not working

I am trying to get the BackupAgent working but I can't get it to work. Here is my sample code:

The layout is just a TextView and a Button.

MainActivity:

...     
public static final String PREF_NAME = "TestPref";
    private static final String TEST_KEY = "TEST";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final SharedPreferences pref = getApplicationContext()
            .getSharedPreferences(PREF_NAME, MODE_PRIVATE);

    if (pref.getString(TEST_KEY, "").length() == 0) {
        pref.edit().putString(TEST_KEY, "new Date())
                .commit();
        new BackupManager(getApplicationContext()).dataChanged();
    }

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            TextView tv = (TextView) findViewById(R.id.textView1);
            if ("START_VALUE".equalsIgnoreCase(tv.getText().toString())) {
                tv.setText(pref.getString(TEST_KEY, ""));
            }
        }
    });
}

The BackupHelper is just the ones I available here: http://developer.android.com/reference/android/app/backup/SharedPreferencesBackupHelper.html I adjusted the name of the pref file with the one I used.

And in the Manifest I added android:backupAgent="TheBackupAgent" (application tag) and the backup-meta data

 <meta-data android:name="com.google.android.backup.api_key"
          android:value="{registered_key}" />

So its really a very simple app.

I am doing the following now:

1)Starting app

2) Textview is initialized with "START_VALUE" in xml file, so I press the Button and the pref-value is displayed

3) I run "adb shell bmgr run" from the console to run the backup immediately

4) I run "adb uninstall com.foo.backuptest"

5) I run "adb install com.foo.backuptest"

Now the value (timestamp) is not restored from the cloud. A new one is generated.

Where is my error??

Upvotes: 3

Views: 2471

Answers (1)

J Rocks
J Rocks

Reputation: 107

Your Manifest file needs to include this to turn on Backup:

android:allowBackup="true"
android:backupAgent="TheBackupAgent"

What about your phone Backup settings? Make sure "Back up my data" and "Automatic restore" are checked and you have entered in a valid Backup Account email id.

To know when and how often data is backuped by Google, take a look at this link: Android backup service - when and how often to backup?

According to this Tester (I also ran a backup frequency test just now): https://advancedweb.hu/2014/12/09/practical_measurement_of_the_android_backup_manager/

The Backup Manager Service responds every hour (I also proved this in my tests) as long as at least one DataChanged() was called in-between the hour since the last data backup request

For a quick test with command line, try these commands:

To ensure Data Backup Enabled: adb shell bmgr enable true

To request a Data Backup: adb shell bmgr backup your.package.name

To initiate a Data Backup: adb shell bmgr run

To uninstall your App: adb uninstall your.package.name

Then install your App: adb install your.package.name

Upvotes: 1

Related Questions