gpt4 book ai didi

java - 具有多组 SharedPreferences 的 BackupAgentHelper

转载 作者:太空宇宙 更新时间:2023-11-04 14:05:56 26 4
gpt4 key购买 nike

我无法理解 addHelper() 方法的 keyPrefix。对于每个 BackupAgentHelper 类实例或每个 SharedPreferencesBackupHelper 是否需要唯一?我想备份两组或多组 SharedPreferences:

示例:

public class PrefsBackupAgent extends BackupAgentHelper {

// Allocate a helper and add it to the backup agent
@Override
public void onCreate() {
SharedPreferencesBackupHelper user1 = new SharedPreferencesBackupHelper(this, "user1_preferences");
addHelper('prefs', user1); // <-- keyPrefix same to both addHelper Calls?

SharedPreferencesBackupHelper user2 = new SharedPreferencesBackupHelper(this, "user2_preferences");
addHelper('prefs', user2); // <--
}
}

或者它是否需要看起来像这样:

public class PrefsBackupAgent extends BackupAgentHelper {

// Allocate a helper and add it to the backup agent
@Override
public void onCreate() {
SharedPreferencesBackupHelper user1 = new SharedPreferencesBackupHelper(this, "user1_preferences");
addHelper('user1', user1); // <-- or do they need to be unique for each SharedPreferencesBackupHelper ?

SharedPreferencesBackupHelper user2 = new SharedPreferencesBackupHelper(this, "user2_preferences");
addHelper('user2', user2); // <--
}
}

哪一种是正确的方法?

谢谢!

最佳答案

你的第一个例子是正确的。

public class MyBackupAgentHelper extends BackupAgentHelper {

static final String DEFAULT_PREFS = "packagename_preferences";
static final String OTHER_PREFS = "packagename_other_preference";

// A key to uniquely identify the set of backup data
static final String PREFS_BACKUP_KEY = "prefs";

@Override
public void onCreate() {
SharedPreferencesBackupHelper helper1 = new SharedPreferencesBackupHelper(this, DEFAULT_PREFS);
SharedPreferencesBackupHelper helper2 = new SharedPreferencesBackupHelper(this, OTHER_PREFS);
addHelper(PREFS_BACKUP_KEY, helper1);
addHelper(PREFS_BACKUP_KEY, helper2);
}
}

关于java - 具有多组 SharedPreferences 的 BackupAgentHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com