Arquivos de preferência compartilhados únicos ou múltiplos
Se você tiver vários Shared Preference
arquivos, esta é uma boa maneira de mostrar todos eles, mas você também pode passar 1 nome de arquivo.
loadSharedPrefs("pref_name");
loadSharedPrefs("shared_pref1", "shared_pref2", "shared_pref3");
Escolha um dos seguintes para atender às suas necessidades ...
Valores de tipo único
public void loadSharedPrefs(String ... prefs) {
// Logging messages left in to view Shared Preferences. I filter out all logs except for ERROR; hence why I am printing error messages.
Log.i("Loading Shared Prefs", "-----------------------------------");
Log.i("----------------", "---------------------------------------");
for (String pref_name: prefs) {
SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
for (String key : preference.getAll().keySet()) {
Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
preference.getString(key, "error!"));
}
Log.i("----------------", "---------------------------------------");
}
Log.i("Finished Shared Prefs", "----------------------------------");
}
Valores de vários tipos
public void loadSharedPrefs(String ... prefs) {
// Define default return values. These should not display, but are needed
final String STRING_ERROR = "error!";
final Integer INT_ERROR = -1;
// ...
final Set<String> SET_ERROR = new HashSet<>(1);
// Add an item to the set
SET_ERROR.add("Set Error!");
// Loop through the Shared Prefs
Log.i("Loading Shared Prefs", "-----------------------------------");
Log.i("------------------", "-------------------------------------");
for (String pref_name: prefs) {
SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
Map<String, ?> prefMap = preference.getAll();
Object prefObj;
Object prefValue = null;
for (String key : prefMap.keySet()) {
prefObj = prefMap.get(key);
if (prefObj instanceof String) prefValue = preference.getString(key, STRING_ERROR);
if (prefObj instanceof Integer) prefValue = preference.getInt(key, INT_ERROR);
// ...
if (prefObj instanceof Set) prefValue = preference.getStringSet(key, SET_ERROR);
Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
String.valueOf(prefValue));
}
Log.i("------------------", "-------------------------------------");
}
Log.i("Loaded Shared Prefs", "------------------------------------");
}
}
Saída Logcat
Meus Shared Preference
valores são todos String
, mas esta é a saída usando um dos 2 métodos acima ...
I/Loading Shared Prefs﹕ -----------------------------------
I/------------------﹕ -------------------------------------
I/Shared Preference : FAVORITE - 135397﹕ Jurassic World
I/Shared Preference : FAVORITE - 87101﹕ Terminator Genisys
I/Shared Preference : FAVORITE - 177677﹕ Mission: Impossible – Rogue Nation
I/------------------﹕ -------------------------------------
I/Shared Preference : WATCHED - 177677﹕ Mission: Impossible – Rogue Nation
I/Shared Preference : WATCHED - 157336﹕ Interstellar
I/Shared Preference : WATCHED - 135397﹕ Jurassic World
I/Shared Preference : WATCHED - 87101﹕ Terminator Genisys
I/------------------﹕ -------------------------------------
I/Shared Preference : WILL_WATCH - 211672﹕ Minions
I/Shared Preference : WILL_WATCH - 102899﹕ Ant-Man
I/------------------﹕ -------------------------------------
I/Loaded Shared Prefs﹕ ------------------------------------