SharedPreferences备份不起作用

时间:2014-03-26 01:39:43

标签: android sharedpreferences android-backup-service

MyBackupAgentHelper.java

public class MyBackupAgentHelper extends BackupAgentHelper {

private static final String MESSAGE_DB_FILE_BACKUP_KEY = "MESSAGE_DB_FILE_BACKUP_KEY";
private static final String DEFAULT_SHARED_PREFRERNCES_KEY = "DEFAULT_SHARED_PREFRERNCES_KEY";

@Override
public void onCreate() {
    super.onCreate();

    LogUtil.pd();

    File file = MessageDao.getSingleton(this).getSQLite().getFile();
    if (file == null) {
        LogUtil.w("Messsage DB File is NULL !");
    } else {
        LogUtil.i("Messsage DB File: "+file.getAbsolutePath());
        FileBackupHelper backupHelper = new FileBackupHelper(this, file.getName());
        addHelper(MESSAGE_DB_FILE_BACKUP_KEY, backupHelper);
    }

    String prefName = getDefaultSharedPreferencesName(this); // It return "my.package.name_preferences"
    LogUtil.i("Default Shared Preferences Name: "+prefName);
    SharedPreferencesBackupHelper backupHelper = new SharedPreferencesBackupHelper(this, prefName);
    addHelper(DEFAULT_SHARED_PREFRERNCES_KEY, backupHelper);
}

Log Cat

03-26 10:11:02.361: I/MyPackage(3540): Messsage DB File: /data/data/my.package.name/files/db.sqlite
03-26 10:11:02.361: I/MyPackage(3540): Default Shared Preferences Name: my.package.name_preferences

数据目录文件树(data / data / my.package.name)

03-26 10:09:22.291: I/MyPackage(3540): my.package.name
03-26 10:09:22.291: I/MyPackage(3540): -shared_prefs
03-26 10:09:22.291: I/MyPackage(3540): --my.package.name_preferences.xml
03-26 10:09:22.291: I/MyPackage(3540): -files
03-26 10:09:22.291: I/MyPackage(3540): --db.sqlite
03-26 10:09:22.291: I/MyPackage(3540): -lib

我被迫通过adb shell进行备份,删除了我的应用,并重新安装了我的应用。

我检查了备份&恢复db.sqlite文件(它工作正常。)

但默认的SharedPreferences未恢复或备份。

我该如何备份&恢复默认的SharedPreferences?

1 个答案:

答案 0 :(得分:-1)

您需要将SharedPreference的权限设置为至少MODE_WORLD_READABLE,默认情况下它是在MODE_PRIVATE中创建的。

点击阅读developer.android.com上的SharedPreference模式的API文档!

如果使用adb backup命令不是MODE_PRIVATE,则可以备份SharedPreference。