Как использовать SharedPreferences в качестве LocalStore, более общим способом?

Быть новичком в мире Android и радостно развиваться день ото дня;) Я хотел бы поделиться примерами общего использования.

Вот пример использования SharedPreferences с универсальным классом LocalStore.

создайте общий класс, который будет использоваться вашим основным действием или любым из вспомогательных действий.

    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;
    }

    }

Примечание : Вы можете использовать ObjectSerializer из здесь

Наслаждайтесь!

Дополнительное обновление: Я реализовал библиотеку для использования MEMDISKCACHE и SHAREDPREF как GENERIC_STORE любой желающий может использовать его с
-> https://github.com/wareninja/generic-store-for-android

9
задан guleryuz 14 February 2011 в 20:28
поделиться