Android之SharedPreferences工具类

本工具类永久维护,永久更新,如果各位读者发现有bug或者不合理之处,请留言,博主将第一时间改正。


本工具类提供的功能有:

1.存储五种类型的数据;

2.读取五种类型的数据;


内容如下:

  1. import android.content.Context;  
  2. import android.content.SharedPreferences;  
  3. import android.content.SharedPreferences.Editor;  
  4.   
  5. /** 
  6.  *  
  7.  * @author bear 
  8.  *  
  9.  *         SharedPrefereces工具类 
  10.  *  
  11.  */  
  12. public class SharedPrefsUtil {  
  13.       
  14.     /** 
  15.      * 向SharedPreferences中写入int类型数据 
  16.      *  
  17.      * @param context 上下文环境 
  18.      * @param name 对应的xml文件名称 
  19.      * @param key 键 
  20.      * @param value 值 
  21.      */  
  22.     public static void putValue(Context context, String name, String key,  
  23.             int value) {  
  24.         Editor sp = getEditor(context, name);  
  25.         sp.putInt(key, value);  
  26.         sp.commit();  
  27.     }  
  28.       
  29.     /** 
  30.      * 向SharedPreferences中写入boolean类型的数据 
  31.      *  
  32.      * @param context 上下文环境 
  33.      * @param name 对应的xml文件名称 
  34.      * @param key 键 
  35.      * @param value 值 
  36.      */  
  37.     public static void putValue(Context context, String name, String key,  
  38.             boolean value) {  
  39.         Editor sp = getEditor(context, name);  
  40.         sp.putBoolean(key, value);  
  41.         sp.commit();  
  42.     }  
  43.       
  44.     /** 
  45.      * 向SharedPreferences中写入String类型的数据 
  46.      *  
  47.      * @param context 上下文环境 
  48.      * @param name 对应的xml文件名称 
  49.      * @param key 键 
  50.      * @param value 值 
  51.      */  
  52.     public static void putValue(Context context, String name, String key,  
  53.             String value) {  
  54.         Editor sp = getEditor(context, name);  
  55.         sp.putString(key, value);  
  56.         sp.commit();  
  57.     }  
  58.       
  59.     /** 
  60.      * 向SharedPreferences中写入float类型的数据 
  61.      *  
  62.      * @param context 上下文环境 
  63.      * @param name 对应的xml文件名称 
  64.      * @param key 键 
  65.      * @param value 值 
  66.      */  
  67.     public static void putValue(Context context, String name, String key,  
  68.             float value) {  
  69.         Editor sp = getEditor(context, name);  
  70.         sp.putFloat(key, value);  
  71.         sp.commit();  
  72.     }  
  73.   
  74.     /** 
  75.      * 向SharedPreferences中写入long类型的数据 
  76.      *  
  77.      * @param context 上下文环境 
  78.      * @param name 对应的xml文件名称 
  79.      * @param key 键 
  80.      * @param value 值 
  81.      */  
  82.     public static void putValue(Context context, String name, String key,  
  83.             long value) {  
  84.         Editor sp = getEditor(context, name);  
  85.         sp.putLong(key, value);  
  86.         sp.commit();  
  87.     }  
  88.       
  89.     /** 
  90.      * 从SharedPreferences中读取int类型的数据 
  91.      *  
  92.      * @param context 上下文环境 
  93.      * @param name 对应的xml文件名称 
  94.      * @param key 键 
  95.      * @param defValue 如果读取不成功则使用默认值 
  96.      * @return 返回读取的值 
  97.      */  
  98.     public static int getValue(Context context, String name, String key,  
  99.             int defValue) {  
  100.         SharedPreferences sp = getSharedPreferences(context, name);  
  101.         int value = sp.getInt(key, defValue);  
  102.         return value;  
  103.     }  
  104.       
  105.     /** 
  106.      * 从SharedPreferences中读取boolean类型的数据 
  107.      *  
  108.      * @param context 上下文环境 
  109.      * @param name 对应的xml文件名称 
  110.      * @param key 键 
  111.      * @param defValue 如果读取不成功则使用默认值 
  112.      * @return 返回读取的值 
  113.      */  
  114.     public static boolean getValue(Context context, String name, String key,  
  115.             boolean defValue) {  
  116.         SharedPreferences sp = getSharedPreferences(context, name);  
  117.         boolean value = sp.getBoolean(key, defValue);  
  118.         return value;  
  119.     }  
  120.       
  121.     /** 
  122.      * 从SharedPreferences中读取String类型的数据 
  123.      *  
  124.      * @param context 上下文环境 
  125.      * @param name 对应的xml文件名称 
  126.      * @param key 键 
  127.      * @param defValue 如果读取不成功则使用默认值 
  128.      * @return 返回读取的值 
  129.      */  
  130.     public static String getValue(Context context, String name, String key,  
  131.             String defValue) {  
  132.         SharedPreferences sp = getSharedPreferences(context, name);  
  133.         String value = sp.getString(key, defValue);  
  134.         return value;  
  135.     }  
  136.       
  137.     /** 
  138.      * 从SharedPreferences中读取float类型的数据 
  139.      *  
  140.      * @param context 上下文环境 
  141.      * @param name 对应的xml文件名称 
  142.      * @param key 键 
  143.      * @param defValue 如果读取不成功则使用默认值 
  144.      * @return 返回读取的值 
  145.      */  
  146.     public static float getValue(Context context, String name, String key,  
  147.             float defValue) {  
  148.         SharedPreferences sp = getSharedPreferences(context, name);  
  149.         float value = sp.getFloat(key, defValue);  
  150.         return value;  
  151.     }  
  152.       
  153.     /** 
  154.      * 从SharedPreferences中读取long类型的数据 
  155.      *  
  156.      * @param context 上下文环境 
  157.      * @param name 对应的xml文件名称 
  158.      * @param key 键 
  159.      * @param defValue 如果读取不成功则使用默认值 
  160.      * @return 返回读取的值 
  161.      */  
  162.     public static long getValue(Context context, String name, String key,  
  163.             long defValue) {  
  164.         SharedPreferences sp = getSharedPreferences(context, name);  
  165.         long value = sp.getLong(key, defValue);  
  166.         return value;  
  167.     }  
  168.       
  169.     //获取Editor实例  
  170.     private static Editor getEditor(Context context, String name) {  
  171.         return getSharedPreferences(context, name).edit();  
  172.     }  
  173.   
  174.     //获取SharedPreferences实例  
  175.     private static SharedPreferences getSharedPreferences(Context context, String name) {  
  176.         return context.getSharedPreferences(name, Context.MODE_PRIVATE);  
  177.     }  
  178. }  

如果你的工程支持版本信息Android API Level >= 11,那么你还可以加入Set<String>类型的存储和读取。


在Activity或者其他的地方调用方式举例如下:

  1. //存储数据,在MySetting文件中存储键值对("color","red")  
  2. SharedPrefsUtil.putValue(this"MySetting""color""red");  
  1. //读取数据,从MySetting文件中读取键为"color"的值  
  2. String color = SharedPrefsUtil.getValue(this"MySetting""color""blue"); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值