7

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?

1
  • plz tell me if you found a solution for this
    – abh22ishek
    Apr 10, 2015 at 6:48

1 Answer 1

6

Your backup agent class for SharedPreferences should be something like this:

public class MyPrefsBackupAgent extends BackupAgentHelper {
    // The name of the SharedPreferences file
    static final String PREFS = "user_preferences";

    // A key to uniquely identify the set of backup data
    static final String PREFS_BACKUP_KEY = "prefs";

    // Allocate a helper and add it to the backup agent
    @Override
    public void onCreate() {
        SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
        addHelper(PREFS_BACKUP_KEY, helper);
    }
}

Then, you must request a cloud backup (it will be done async) with something like:

import android.app.backup.BackupManager;
 ...

 public void requestBackup() {
   BackupManager bm = new BackupManager(this);
   bm.dataChanged();
 }

and you don't need to manually restore your SharedPreferences as they are automagically managed by the SharedPreferencesBackupHelper class.

Besides your backup API key, don't forget to add your backup agent class in your manifest:

<application android:label="MyApplication"
             android:backupAgent="MyBackupAgent">

More info about all this at http://developer.android.com/guide/topics/data/backup.html and http://developer.android.com/training/cloudsync/backupapi.html

3
  • I will check it as soon as possible and report the result. Sep 4, 2013 at 15:42
  • Does it work? I have all these properly setup. Still not working.
    – Bagusflyer
    Feb 27, 2015 at 5:41
  • 1
    Where String PREFS = "user_preferences"; don't forget to change user_preferences to the name of your actual preference file. i.e the one defined in getSharedPreferences("AppName", 0);
    – patel8786
    Dec 31, 2018 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.