Search code examples
android-6.0-marshmallowandroid-backup-service

Exclude specific sharedpreference key with Android 6.0 auto backup


I have implemented the "old" GCM implementation where the sample code had the following:

public static final String PROPERTY_REG_ID = "registration_id";
private SharedPreferences getGCMPreferences(Context context) {
    return context.getSharedPreferences(SampleApp.class.getSimpleName(),
            Context.MODE_PRIVATE);
}
...
String registrationId = prefs.getString(PROPERTY_REG_ID, "");

With the new backup system in Android 6.0 it says you should exclude this key but the exclude format docs: http://developer.android.com/training/backup/autosyncapi.html

doesn't really seem to indicate how you can exclude a sharedpreference except saying that:

sharedpref: Specifies a SharedPreferences object that the getSharedPreferences() method returns.

There isn't a getSharedPreferences() with no parameters to my knowledge?

I tried:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
  <exclude domain="sharedpref" path="registration_id"/>
</full-backup-content>

But that didn't seem to work naturally since I haven't indicated which sharedpreference file it should exclude from. Anyone successfully implemented this?


Solution

  • The exclusion is for a shared preferences file, not a single key within the file.

    (In your example, your filename is got via SampleApp.class.getSimpleName().)

    As the comment points out, you need to specify a full filename, so remember to include the ".xml" file extension when you put the name in the exclude instruction.