使用Android 6.0自动备份排除特定的共享首选项密钥

Chr*_*vik 7 android-backup-service android-6.0-marshmallow

我已经实现了"旧的"GCM实现,其中示例代码具有以下内容:

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, "");
Run Code Online (Sandbox Code Playgroud)

使用Android 6.0中的新备份系统,它表示您应该排除此密钥,但排除格式文档:http: //developer.android.com/training/backup/autosyncapi.html

似乎并没有真正表明如何排除共享偏好,除了说:

sharedpref:指定getSharedPreferences()方法返回的SharedPreferences对象.

根据我的知识,没有没有参数的getSharedPreferences()?

我试过了:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
  <exclude domain="sharedpref" path="registration_id"/>
</full-backup-content>
Run Code Online (Sandbox Code Playgroud)

但这似乎并不自然,因为我没有指出它应该排除哪个共享首选项文件.有人成功实现了吗?

zma*_*ies 9

排除是针对共享首选项文件,而不是文件中的单个键.

(在您的示例中,您的文件名是通过SampleApp.class.getSimpleName().)

正如评论指出的那样,您需要指定完整的文件名,因此在将名称放入exclude指令时请记住包含".xml"文件扩展名.

  • 我只是想为我添加这还不够.我不得不在末尾添加扩展名".xml"以使其工作.通过例子排除context.getSharedPreferences("test",Context.MODE_PRIVATE),我不得不写<exclude domain ="sharedpref"path ="test.xml"/> (4认同)