Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 772
Christer Nordvik Avatar asked Oct 29 '15 08:10

Christer Nordvik


1 Answers

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.

like image 198
zmarties Avatar answered Nov 04 '22 07:11

zmarties