美文网首页
SharedPreferences 工具类,本地储存数据

SharedPreferences 工具类,本地储存数据

作者: 小江yue | 来源:发表于2018-03-16 11:53 被阅读14次

    public class SPUtils {

         private static SharedPreferences getSharePreferences(Context context) {

              return context.getSharedPreferences("WINE_BUYER_PREFERENCES", Context.MODE_PRIVATE);

         }

    //   储存字符串数据

         public static void setBoolean(Context context, String key, boolean value) { 

              SharedPreferences sharePreferences = getSharePreferences(context); 

              SharedPreferences.Editor edit = sharePreferences.edit();

              edit.putBoolean(key, value); edit.apply();

         }

    //   获取布尔值数据

         public static boolean getBoolean(Context context, String key) {

              SharedPreferences preferences = getSharePreferences(context);

              return preferences.getBoolean(key, false);

         }

    // 获取字符串数据

         public static void setString(Context context, String key, String value) {

              SharedPreferences sharePreferences = getSharePreferences(context);

              SharedPreferences.Editor edit = sharePreferences.edit();

              edit.putString(key, value); edit.apply();

         }

    //   储存布尔值数据

         public static String getString(Context context, String key) {

              SharedPreferences sharePreferences = getSharePreferences(context);

              return sharePreferences.getString(key, "");

         }

    }

    相关文章

      网友评论

          本文标题:SharedPreferences 工具类,本地储存数据

          本文链接:https://www.haomeiwen.com/subject/vjmlqftx.html