androidsharedpreferencesandroid-backup-service

Using Android Backup Service to backup my SharedPreferences


My intention is to implement a backup service for my application, storing within the cloud configuration properties when changes are made, and retrieving them when the application first starts.

But I'm having some problems: the backup process is not made correctly.

My Android Manifest contains the following:

<application
    ...
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    android:allowBackup="true"
    android:backupAgent=".backup.BizkaimoveBackup">
        ...

        <!-- Android Backup -->
        <meta-data 
            android:name="com.google.android.backup.api_key" 
            android:value="--key--" />
        <!-- Android Backup -->

The BackupAgentHelper class contains:

public class BizkaimoveBackup extends BackupAgentHelper {

    /*
     * Atributos
     */
    private static String LOG_TAG = "BizkaimoveBackup";
    private SharedPreferencesBackupHelper spbh;

    /**
     * El nombre del fichero de SharedPreferences: el mismo que hay en globales.
     */
    private static String PREFS = "bizkaimovePrefs";

    /**
     * Una clave para identificar unequívocamente un conjunto de datos de backup
     */
    private static final String PREFS_BACKUP_KEY = "myprefs";

    /*
     * Métodos
     */
    @Override
    public void onCreate() {
        spbh = new SharedPreferencesBackupHelper(this, PREFS);
        addHelper(PREFS, spbh);
        Log.d(LOG_TAG, "Añadiendo BackupAgent...");
    }

}

To store changes in the cloud, I do the following:

switchPreferenceIdiomaEs = (SwitchPreference) findPreference("pref_switch_idioma_es");
    switchPreferenceIdiomaEu = (SwitchPreference) findPreference("pref_switch_idioma_eu");

    switchPreferenceIdiomaEs.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            //newValue es boolean aquí
            SharedPreferences sp = getActivity().getSharedPreferences("bizkaimovePrefs", Context.MODE_PRIVATE);
            Editor spEditor = sp.edit();
            spEditor.putBoolean("idiomaEsCastellano", (Boolean) newValue);
            spEditor.putBoolean("idiomaEsEuskera", !((Boolean) newValue));
            spEditor.commit();
            switchPreferenceIdiomaEu.setChecked(!((Boolean) newValue));

            BackupManager.dataChanged("com.ingartek.bizkaimove");

            return true;
        }
    });

The XML preferencias.xml file contains the following:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:key="bizkaimovePrefs"
    android:persistent="true"
    android:id="@+id/listaPrefs">

    <PreferenceCategory
        android:title="@string/config_idioma">

        <!-- Un switch para euskera, otro para castellano -->
        <SwitchPreference 
            android:key="pref_switch_idioma_eu"
            android:title="@string/config_euskera"
            android:switchTextOn="@string/config_euskera"
            android:switchTextOff="@string/config_castellano"/>

        <SwitchPreference 
            android:key="pref_switch_idioma_es"
            android:title="@string/config_castellano"
            android:switchTextOn="@string/config_castellano"
            android:switchTextOff="@string/config_euskera"/>

    </PreferenceCategory>
    ...

And finally, within the onCreate() of the HomeActivity I do the following:

BackupManager bm = new BackupManager(this);
        bm.requestRestore(new RestoreObserver() {

            @Override
            public void restoreStarting(int numPackages) {
                Toast.makeText(getApplicationContext(), "Empezando recuperación de backup...", Toast.LENGTH_SHORT).show();
                super.restoreStarting(numPackages);
            }

            @Override
            public void restoreFinished(int error) {
                Toast.makeText(getApplicationContext(), "Recuperación de backup finalizada. Error: " + error, Toast.LENGTH_SHORT).show();
                super.restoreFinished(error);

However, after doing the actions described here http://developer.android.com/guide/topics/data/backup.html#Testing no data is retrieved after uninstalling my app.

I have been able to download and test this https://bitbucket.org/andreaskristensson/android-example-preference-fragment-and-backup-api/downloads but neither it isn't working.

What am I doing incorrectly?

Thanks in advance.


Solution

  • Finally I realised what my problem was...

    I created another file and some preferences were stored there, and the rest were saved within the default preference XML file.

    So the backup manager was ONLY working with one of them.