我一直在尝试在我的 Android 应用程序中备份我的 sharedpreferences 文件,但到目前为止还没有。我正在使用开发人员指南中的简单 Google 代码。下面是MyPrefsBackup
该类的代码。
public class MyPrefsBackup extends BackupAgentHelper {
// The name of the SharedPreferences file
static final String PREFS = "UserDB";
// A key to uniquely identify the set of backup data
static final String PREFS_BACKUP_KEY = "prefs";
// Allocate a helper and add it to the backup agent
public void onCreate() {
SharedPreferencesBackupHelper helper = new SharedPreferencesBackupHelper(this, PREFS);
addHelper(PREFS_BACKUP_KEY, helper);
}
我想我终于意识到,PREFS_BACKUP_KEY
实际上必须是我存储数据的特定键。我只是使用“prefs”,所以我认为这就是为什么没有备份数据的原因。但是,我在SharedPreferences
文件中存储了相当多的数据,所以如何在SharedPreferences
不指定每个单独密钥的情况下继续保存整个文件。(一些键是由应用程序生成的,所以我什至不知道它们是否正在使用,直到用户输入他们的数据)。
我想知道有没有办法告诉BackupHelper
班级备份整个SharedPreferences
文件?