Siendo nuevo en el mundo de Android y avanzando con alegría día a día;) Me gustaría compartir ejemplos sobre el uso común.¿Cómo usar SharedPreferences como LocalStore, de una manera más genérica?
Aquí viene un ejemplo sobre el uso de SharedPreferences con la clase LocalStore genérica.
crea una clase común para ser utilizada por tu actividad principal o por cualquiera de las subactividades.
public class LocalStore {
private static final String TAG = "LocalStore";
private static final String PREF_FILE_NAME = "userprefs";
public static void clear(Context context) {
clear(context, "unknown");
}
public static void clear(Context context, String caller) {
Editor editor =
context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
Log.d(TAG, "caller:"+caller + "|clear LocalStore");
}
public static boolean setCustomBooleanData(String key, boolean value, Context context) {
Editor editor =
context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
editor.putBoolean(key, value);
return editor.commit();
}
public static boolean getCustomBooleanData(String key, Context context) {
SharedPreferences savedSession =
context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
return (savedSession.getBoolean(key, false));
}
public static boolean setCustomStringData(String key, String value, Context context) {
Editor editor =
context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
editor.putString(key, value);
return editor.commit();
}
public static String getCustomStringData(String key, Context context) {
SharedPreferences savedSession =
context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
return (savedSession.getString(key, null));
}
public static boolean isCustomStringExistInLocal(String customKey, Context context) {
SharedPreferences savedSession =
context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
return (savedSession.getString(customKey, null))==null?false:true;
}
public static boolean saveObject(String objKey, Serializable dataObj, Context context) {
Editor editor =
context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
editor.putString(objKey, ObjectSerializer.serialize(dataObj));
Log.d(TAG, "savedObject| objKey:"+objKey+"/" + dataObj.toString());
return editor.commit();
}
public static Object getObject(String objKey, Context context) {
SharedPreferences savedSession =
context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
Object dataObj = ObjectSerializer.deserialize(savedSession.getString(objKey, null));
return dataObj;
}
}
Nota: Puede utilizar ObjectSerializer de here
disfrutar!
actualización adicional: que implementó una biblioteca para usar MEMDISKCACHE y SHAREDPREF como GENERIC_STORE cualquier persona interesada puede usarlo desde
->https://github.com/wareninja/generic-store-for-android
stackoverflow FAQ dice: "También es perfectamente posible hacer y contestar su propia pregunta, siempre y cuando pretendas que estás en Jeopardy: frasealo en forma de pregunta ". –
Podría hacer que esta sea una pregunta y respuesta adecuada en lugar de que todo esté en el cuerpo de la pregunta. De lo contrario, puede eliminarse. – Kev
Es bueno que comparta :), pero creo que esto pertenece a un blog en lugar de a un sitio de preguntas y respuestas. –