9

作为 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使用它

4

2 回答 2

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

因此,您可以像这样简单地调用重载函数:

setData(this, "myBoolean", true);
setData(this, "myString", "Its Awesome");
于 2012-09-12T12:09:19.133 回答
0

像这样

 fun <T> putData(key: String?, item: T) {
        val editor = localSharedPreferences.edit()
        val list = gson.toJson(item)
        editor.putString(key, list)
        editor.apply()
    }
于 2020-08-08T21:55:40.590 回答