7

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?

Christer Nordvik
  • 2,518
  • 3
  • 35
  • 52

1 Answers1

10

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.

zmarties
  • 4,809
  • 22
  • 39
  • 4
    I just want to add that for me it wasn't enough. I had to add the extension ".xml" at the end to make it work. By exemple to exclude context.getSharedPreferences("test", Context.MODE_PRIVATE), I had to write – Esteam Nov 03 '15 at 09:20
  • @Esteam The same thing as provided in [this comment](https://code.google.com/p/android-developer-preview/issues/detail?id=2569#c1), he tries to exclude `gcm` shared reference file and added `gcm.xml` as a path. – Mohammed AlBanna Dec 18 '15 at 11:49
  • Why is it needed though, to exclude the preferences files? I thought that's the whole point of backup&restore... To get back to the state the user had before... – android developer Nov 24 '18 at 16:50
  • @androiddeveloper and anyone else coming across this in the future, the reason why you might want to exclude specific things from the backup is because it doesn't make sense to restore them (tokens that will have expired or other things that the user doesn't directly control) – undermark5 May 05 '21 at 22:39
  • @undermark5 I see. Can you please tell me how the path should be like there? I want to know how to exclude both specific SharedPrferences files and specific DB files. Do other files also get restored? Or just these? – android developer May 06 '21 at 11:41