zoukankan      html  css  js  c++  java
  • 对SharePreference的封装

    今天需要用到SharePreference来保存一些设置参数,因为要用到很多次 所以对它进行了封装:

    public class PrefUtils {
    
        public static void putBoolean(String key, boolean value, Context ctx) {
            SharedPreferences sp = ctx.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
            sp.edit().putBoolean(key, value).commit();
        }
    
        public static boolean getBoolean(String key, boolean defValue, Context ctx) {
            SharedPreferences sp = ctx.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
            return sp.getBoolean(key, defValue);
        }
    
        public static void putString(String key, String value, Context ctx) {
            SharedPreferences sp = ctx.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
            sp.edit().putString(key, value).commit();
        }
    
        public static String getString(String key, String defValue, Context ctx) {
            SharedPreferences sp = ctx.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
            return sp.getString(key, defValue);
        }
    
        public static void putInt(String key, int value, Context ctx) {
            SharedPreferences sp = ctx.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
            sp.edit().putInt(key, value).commit();
        }
    
        public static int getInt(String key, int defValue, Context ctx) {
            SharedPreferences sp = ctx.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
            return sp.getInt(key, defValue);
        }
    
        public static void remove(String key, Context ctx) {
            SharedPreferences sp = ctx.getSharedPreferences("config",
                    Context.MODE_PRIVATE);
            sp.edit().remove(key).commit();
        }
    }
  • 相关阅读:
    oracle的 listagg() WITHIN GROUP () 行转列函数的使用
    redis-配置文件详解
    Redis下载安装步骤及操作命令
    探索linux好玩的东西
    发送邮件的工具类
    微信小程序 云开发天然鉴权获取手机号
    JS 设计模式 -代理模式
    语言之魂——原型模式
    微信小程序 云开发中部分用户获取不到unionId
    js封装三级联动
  • 原文地址:https://www.cnblogs.com/AceIsSunshineRain/p/5185178.html
Copyright © 2011-2022 走看看