6

I'm trying to use SharedPreferencesBackupHelper to save my SharedPreferences value to cloud.

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:backupAgent=".DataBackupAgent"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

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

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

DataBackupAgent.java:

public class DataBackupAgent extends BackupAgentHelper {

public static final String PREFS = "data_prefs";
public static final String PREFS_BACKUP_KEY = "myprefs";

@Override
public void onCreate() {
    SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
    addHelper(PREFS_BACKUP_KEY, helper);
}

}

MainActivity.java

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

    backupManager = new BackupManager(this);
    prefs = getSharedPreferences(DataBackupAgent.PREFS, Context.MODE_PRIVATE);
    edit = prefs.edit();

    text = (EditText)findViewById(R.id.editText);
    String value = prefs.getString(DataBackupAgent.PREFS_BACKUP_KEY,"");
    text.setText(value);

    Button btnBackup = (Button)findViewById(R.id.button);
    btnBackup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            edit.putString(DataBackupAgent.PREFS_BACKUP_KEY,text.getText().toString());
            edit.commit();
            backupManager.dataChanged();
        }
    });
}

My steps:

  1. Write something at the EditText, click Backup button
  2. Close app and open again. The saved value will be shown in EditText
  3. Uninstall the app and reinstall again. The saved value is not shown in EditText at all.

Edit at 27/02/2015:

I added the following code to restore manually:

    backupManager.requestRestore(new RestoreObserver() {
        @Override
        public void restoreFinished(int error) {
            super.restoreFinished(error);

            String value = prefs.getString(DataBackupAgent.PREFS_BACKUP_KEY,"");
            text.setText(value);
        }

        @Override
        public void restoreStarting(int numPackages) {
            super.restoreStarting(numPackages);
        }

        @Override
        public void onUpdate(int nowBeingRestored, String currentPackage) {
            super.onUpdate(nowBeingRestored, currentPackage);
        }
    });

Unfortunately no callback functions are called.

This means back or auto restore doesn't work at all. Any idea? Thanks

Bagusflyer
  • 12,675
  • 21
  • 96
  • 179

1 Answers1

0

My steps: 1. Write something at the EditText, click Backup button 2. Close app and open again. The saved value will be shown in EditText 3. Uninstall the app and reinstall again. The saved value is not shown in EditText at all.

To test your implementation there are others steps related to the use of bmgras we can see here.

Nevertheless I implemented this feature some days ago and following the steps in the documentation using a real device - Samsung SII - the automatic restore doesn't happen BUT using the emulator all was fine.

Logcat will show you all the operation output details.

IMO, the Android Data Backup feature is not reliable today. We can see some discussion about the implementation problems here and here.

Hope it helps!

Community
  • 1
  • 1
ARNeto
  • 441
  • 4
  • 8
  • Thanks for your reply. But I can't use **bmgr** in real application, right? For me, both emulator and devices are not working. Thanks for your reminding. It seems the this method is not reliable at all. Maybe should find another solution. – Bagusflyer Feb 27 '15 at 03:48
  • @bagusflyer You need to use `bmgr` only for tests purposes as an way to say "Ok, my implementation is correct!". You're right the Android auto backup is not reliable. I'll update my answer to clarify these points. My app has already an SQLite database with backup features. I'm considering about migration of `SharedPreferences` values to this database but I'm not sure yet. – ARNeto Feb 27 '15 at 11:18
  • thanks. Maybe I have to think about other solution. My requirement is very simple, just want to save something for example, downloaded id in cloud so that I can refer it back later. – Bagusflyer Mar 03 '15 at 07:53