I would like to backup a value in SharedPreferences so that I can read out this value after a reinstall.
My code does not work and I do not know what is the mistake.
MyBackupAgent
package com.app.appname;
import android.app.backup.BackupAgentHelper;
import android.app.backup.BackupManager;
import android.app.backup.SharedPreferencesBackupHelper;
import android.content.Context;
public class MyBackupAgent extends BackupAgentHelper{
static final String PREFS_DISPLAY = "AppName";
private Context context;
static final String MY_PREFS_BACKUP_KEY = "keyToStore";
public MyBackupAgent(Context context){
this.context = context;
SharedPreferencesBackupHelper helper =
new SharedPreferencesBackupHelper(context, PREFS_DISPLAY);
addHelper(MY_PREFS_BACKUP_KEY, helper);
}
public void storeData(){
BackupManager backupManager = new BackupManager(context);
backupManager.dataChanged();
}
}
How I store the data:
...
SharedPreferences settings = getSharedPreferences("AppName", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("keyToStore", true);
editor.commit();
new MyBackupAgent(this).storeData();
...
How I receive the data:
...
SharedPreferences settings = getSharedPreferences("AppName", 0);
boolean value = settings.getBoolean("keyToStore", false);
...
I also added the API in the Android Manifest:
<application ...>
<meta-data android:name="com.google.android.backup.api_key" android:value="xxxxxxxxxxxxxxxxxxxxxxxxxx" />
Do you have any idea what I am doing wrong and how it works? Does it really work?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…