首页 > 代码库 > android 工具类之SharePreference

android 工具类之SharePreference

 1 /**  2  * SharedPreferences的一个工具类,调用setParam就能保存String, Integer, Boolean, Float, Long类型的参数  3  * 同样调用getParam就能获取到保存在手机里面的数据  4  * @author xiaanming  5  *  6  */   7 public class SharedPreferencesUtils {   8     /**  9      * 保存在手机里面的文件名 10      */  11     private static final String FILE_NAME = "share_date";  12       13       14     /** 15      * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法 16      * @param context 17      * @param key 18      * @param object  19      */  20     public static void setParam(Context context , String key, Object object){  21           22         String type = object.getClass().getSimpleName();  23         SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);  24         SharedPreferences.Editor editor = sp.edit();  25           26         if("String".equals(type)){  27             editor.putString(key, (String)object);  28         }  29         else if("Integer".equals(type)){  30             editor.putInt(key, (Integer)object);  31         }  32         else if("Boolean".equals(type)){  33             editor.putBoolean(key, (Boolean)object);  34         }  35         else if("Float".equals(type)){  36             editor.putFloat(key, (Float)object);  37         }  38         else if("Long".equals(type)){  39             editor.putLong(key, (Long)object);  40         }  41           42         editor.commit();  43     }  44       45       46     /** 47      * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值 48      * @param context 49      * @param key 50      * @param defaultObject 51      * @return 52      */  53     public static Object getParam(Context context , String key, Object defaultObject){  54         String type = defaultObject.getClass().getSimpleName();  55         SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);  56           57         if("String".equals(type)){  58             return sp.getString(key, (String)defaultObject);  59         }  60         else if("Integer".equals(type)){  61             return sp.getInt(key, (Integer)defaultObject);  62         }  63         else if("Boolean".equals(type)){  64             return sp.getBoolean(key, (Boolean)defaultObject);  65         }  66         else if("Float".equals(type)){  67             return sp.getFloat(key, (Float)defaultObject);  68         }  69         else if("Long".equals(type)){  70             return sp.getLong(key, (Long)defaultObject);  71         }  72           73         return null;  74     }  75 }  

保存数据:

    SharedPreferencesUtils.setParam(this, "String", "xiaanming");      SharedPreferencesUtils.setParam(this, "int", 10);      SharedPreferencesUtils.setParam(this, "boolean", true);      SharedPreferencesUtils.setParam(this, "long", 100L);      SharedPreferencesUtils.setParam(this, "float", 1.1f);  

获取数据:

1     SharedPreferencesUtils.getParam(TimerActivity.this, "String", "");                                                                                        SharedPreferencesUtils.getParam(TimerActivity.this, "int", 0);  2     SharedPreferencesUtils.getParam(TimerActivity.this, "boolean", false);  3     SharedPreferencesUtils.getParam(TimerActivity.this, "long", 0L);  4     SharedPreferencesUtils.getParam(TimerActivity.this, "float", 0.0f);