zoukankan      html  css  js  c++  java
  • SharedPreferences-PreferenceUtils

    SharedPreferences   easy use

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.preference.PreferenceManager;
    
    public class PreferenceUtils {
        public static String getPrefString(Context context, String key, final String defaultValue) {
            final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            return settings.getString(key, defaultValue);
        }
    
        public static void setPrefString(Context context, final String key, final String value) {
            final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            settings.edit().putString(key, value).commit();
        }
    
        public static boolean getPrefBoolean(Context context, final String key,
                final boolean defaultValue) {
            final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            return settings.getBoolean(key, defaultValue);
        }
    
        public static boolean hasKey(Context context, final String key) {
            return PreferenceManager.getDefaultSharedPreferences(context).contains(key);
        }
    
        public static void setPrefBoolean(Context context, final String key, final boolean value) {
            final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            settings.edit().putBoolean(key, value).commit();
        }
    
        public static void setPrefInt(Context context, final String key, final int value) {
            final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            settings.edit().putInt(key, value).commit();
        }
    
        public static int getPrefInt(Context context, final String key, final int defaultValue) {
            final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            return settings.getInt(key, defaultValue);
        }
    
        public static void setPrefFloat(Context context, final String key, final float value) {
            final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            settings.edit().putFloat(key, value).commit();
        }
    
        public static float getPrefFloat(Context context, final String key, final float defaultValue) {
            final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            return settings.getFloat(key, defaultValue);
        }
    
        public static void setSettingLong(Context context, final String key, final long value) {
            final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            settings.edit().putLong(key, value).commit();
        }
    
        public static long getPrefLong(Context context, final String key, final long defaultValue) {
            final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            return settings.getLong(key, defaultValue);
        }
    
        public static void clearPreference(Context context, final SharedPreferences p) {
            final Editor editor = p.edit();
            editor.clear();
            editor.commit();
        }
    }
  • 相关阅读:
    5G NR系列(四)物理下行共享信道(PDSCH)物理层过程详解
    5G NR系列(三)PDSCH的解调参考信号(DM-RS)
    Mac上重装pycharm打不开的解决方法
    Oracle parallel理解
    V$ASM_DISKGROUP视图信息解读
    深入了解 Oracle Flex ASM 及其优点
    使用typora和印象笔记高效输出
    Centos7.6部署k8s 集群
    DBA日常职责
    利用DCLI命令实现跨机器检查
  • 原文地址:https://www.cnblogs.com/flyingsir/p/3656795.html
Copyright © 2011-2022 走看看