zoukankan      html  css  js  c++  java
  • SharedPreferences的工具类

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    
    /**
    * SharedPreferences的工具类
    * @author wangfubin
    */
    public class Sp {
    
            private static String name = "config";
    
            /**
             * 获取SharedPreferences实例对象
             *
             * @param context
             * @return
             */
            public static SharedPreferences getSharedPreference(Context context) {
                    return context.getSharedPreferences(name, Context.MODE_PRIVATE);
            }
    
            /**
             * 保存一个Boolean类型的值!
             * @param context
             * @param key
             * @param value
             * @return
             */
            public static boolean putBoolean(Context context, String key, Boolean value) {
                    SharedPreferences sharedPreference = getSharedPreference(context);
                    Editor editor = sharedPreference.edit();
                    editor.putBoolean(key, value);
                    return editor.commit();
            }
            /**
             * 保存一个int类型的值!
             * @param context
             * @param key
             * @param value
             * @return
             */
            public static boolean putInt(Context context, String key, int value) {
                    SharedPreferences sharedPreference = getSharedPreference(context);
                    Editor editor = sharedPreference.edit();
                    editor.putInt(key, value);
                    return editor.commit();
            }
            /**
             * 保存一个float类型的值!
             * @param context
             * @param key
             * @param value
             * @return
             */
            public static boolean putFloat(Context context, String key, float value) {
                    SharedPreferences sharedPreference = getSharedPreference(context);
                    Editor editor = sharedPreference.edit();
                    editor.putFloat(key, value);
                    return editor.commit();
            }
            /**
             * 保存一个long类型的值!
             * @param context
             * @param key
             * @param value
             * @return
             */
            public static boolean putLong(Context context, String key, long value) {
                    SharedPreferences sharedPreference = getSharedPreference(context);
                    Editor editor = sharedPreference.edit();
                    editor.putLong(key, value);
                    return editor.commit();
            }
            /**
             * 保存一个String类型的值!
             * @param context
             * @param key
             * @param value
             * @return
             */
            public static boolean putString(Context context, String key, String value) {
                    SharedPreferences sharedPreference = getSharedPreference(context);
                    Editor editor = sharedPreference.edit();
                    editor.putString(key, value);
                    return editor.commit();
            }
    
            /**
             * 获取String的value
             *
             * @param context
             * @param key
             * 名字
             * @param defValue
             * 默认值
             * @return
             */
            public static String getString(Context context, String key, String defValue) {
                    SharedPreferences sharedPreference = getSharedPreference(context);
                    return sharedPreference.getString(key, defValue);
            }
    
            /**
             * 获取int的value
             *
             * @param context
             * @param key
             * 名字
             * @param defValue
             * 默认值
             * @return
             */
            public static int getInt(Context context, String key, int defValue) {
                    SharedPreferences sharedPreference = getSharedPreference(context);
                    return sharedPreference.getInt(key, defValue);
            }
    
            /**
             * 获取float的value
             *
             * @param context
             * @param key
             * 名字
             * @param defValue
             * 默认值
             * @return
             */
            public static float getFloat(Context context, String key, Float defValue) {
                    SharedPreferences sharedPreference = getSharedPreference(context);
                    return sharedPreference.getFloat(key, defValue);
            }
    
            /**
             * 获取boolean的value
             *
             * @param context
             * @param key
             * 名字
             * @param defValue
             * 默认值
             * @return
             */
            public static boolean getBoolean(Context context, String key,
                            Boolean defValue) {
                    SharedPreferences sharedPreference = getSharedPreference(context);
                    return sharedPreference.getBoolean(key, defValue);
            }
    
            /**
             * 获取long的value
             *
             * @param context
             * @param key
             * 名字
             * @param defValue
             * 默认值
             * @return
             */
            public static long getLong(Context context, String key, long defValue) {
                    SharedPreferences sharedPreference = getSharedPreference(context);
                    return sharedPreference.getLong(key, defValue);
            }
    
    }
  • 相关阅读:
    Git修改注释
    数组和切片的区别
    SpringBoot启动报:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    eclipse中web.xml报:The content of element type "web-app" must match "(icon?,display- name?,
    eclipse错误: 在类xxx中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application
    idea Cannot resolve method ‘setAttribute(java.lang.String, java.lang.String)/不能使用pageContext.setAttribute()
    解决Nginx启动报nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    数据仓库开发规范
    详解会话技术cookie、session和token
    Requests爬虫包及解析工具 xpath、正则、Beautiful Soup
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/3502922.html
Copyright © 2011-2022 走看看