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();
        }
    }
  • 相关阅读:
    vue开发(一)安装
    Ubuntu18.04安装mysql
    使用.NET Framework开发IIS 7.0模块和处理程序拦截请求实现跳转
    Mysql 清空数据后,释放硬盘文件
    依赖注入
    ubuntu 上开发.netcore
    使用python获取微医数据
    Mysql查询某字段重复值并删除重复值
    使用pyinstaller 打包python程序
    堆(heap)和栈(stack)、内存泄漏(memory leak)和内存溢出
  • 原文地址:https://www.cnblogs.com/AceIsSunshineRain/p/5185178.html
Copyright © 2011-2022 走看看