如何以更通用的方式使用SharedPreferences作为LocalStore?

gul*_*yuz 9 android sharedpreferences

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

    }
Run Code Online (Sandbox Code Playgroud)

注意:您可以从此处使用ObjectSerializer

请享用!

附加更新:我实现了一个库,使用MEMDISKCACHE和SHAREDPREF作为GENERIC_STORE,任何感兴趣的人都可以使用它
- > https://github.com/wareninja/generic-store-for-android

Uma*_*air 2

假设您需要一些有关如何进一步改进它的技巧,就在这里。

  • 通常 Android 遵循约定将Context变量保留为第一个参数。创建任何通用库时最好这样做。
  • 如果你想让它更通用,为什么不尝试方法重载呢?它将为开发人员在设置值时提供更大的灵活性,就像在类中处理额外内容一样Intent

例如:

public static boolean setData(Context, String key, boolean value) {
        Editor editor = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
        editor.putBoolean(key, value);
        return editor.commit(); 
    }
public static boolean setData(Context, String key, String value) {
        Editor editor = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
        editor.putString(key, value);
        return editor.commit(); 
    }
Run Code Online (Sandbox Code Playgroud)

所以你可以像这样简单地调用重载函数:

setData(this, "myBoolean", true);
setData(this, "myString", "Its Awesome");
Run Code Online (Sandbox Code Playgroud)