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??