Android之SharedPreferences两个工具类

     相信Android的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数据的时候,就可以直接通过工具类的set和get方法来完成,类似JavaBean,这样使用起来就比较方便,快捷(建议项目中使用次数比较多使用)。好了,直接看看这段简单的代码吧:

[java]  view plain  copy
  1. public class SharePreferenceUtil {  
  2.     private SharedPreferences sp;  
  3.     private SharedPreferences.Editor editor;  
  4.   
  5.     public SharePreferenceUtil(Context context, String file) {  
  6.         sp = context.getSharedPreferences(file, context.MODE_PRIVATE);  
  7.         editor = sp.edit();  
  8.     }  
  9.   
  10.     // 用户的密码  
  11.     public void setPasswd(String passwd) {  
  12.         editor.putString("passwd", passwd);  
  13.         editor.commit();  
  14.     }  
  15.   
  16.     public String getPasswd() {  
  17.         return sp.getString("passwd""");  
  18.     }  
  19.   
  20.     // 用户的id,即QQ号  
  21.     public void setId(String id) {  
  22.         editor.putString("id", id);  
  23.         editor.commit();  
  24.     }  
  25.   
  26.     public String getId() {  
  27.         return sp.getString("id""");  
  28.     }  
  29.   
  30.     // 用户的昵称  
  31.     public String getName() {  
  32.         return sp.getString("name""");  
  33.     }  
  34.   
  35.     public void setName(String name) {  
  36.         editor.putString("name", name);  
  37.         editor.commit();  
  38.     }  
  39.   
  40.     // 用户的邮箱  
  41.     public String getEmail() {  
  42.         return sp.getString("email""");  
  43.     }  
  44.   
  45.     public void setEmail(String email) {  
  46.         editor.putString("email", email);  
  47.         editor.commit();  
  48.     }  
  49.   
  50.     // 用户自己的头像  
  51.     public Integer getImg() {  
  52.         return sp.getInt("img"0);  
  53.     }  
  54.   
  55.     public void setImg(int i) {  
  56.         editor.putInt("img", i);  
  57.         editor.commit();  
  58.     }  
  59.   
  60.     // ip  
  61.     public void setIp(String ip) {  
  62.         editor.putString("ip", ip);  
  63.         editor.commit();  
  64.     }  
  65.   
  66.     public String getIp() {  
  67.         return sp.getString("ip", Constants.SERVER_IP);  
  68.     }  
  69.   
  70.     // 端口  
  71.     public void setPort(int port) {  
  72.         editor.putInt("port", port);  
  73.         editor.commit();  
  74.     }  
  75.   
  76.     public int getPort() {  
  77.         return sp.getInt("port", Constants.SERVER_PORT);  
  78.     }  
  79.   
  80.     // 是否在后台运行标记  
  81.     public void setIsStart(boolean isStart) {  
  82.         editor.putBoolean("isStart", isStart);  
  83.         editor.commit();  
  84.     }  
  85.   
  86.     public boolean getIsStart() {  
  87.         return sp.getBoolean("isStart"false);  
  88.     }  
  89.   
  90.     // 是否第一次运行本应用  
  91.     public void setIsFirst(boolean isFirst) {  
  92.         editor.putBoolean("isFirst", isFirst);  
  93.         editor.commit();  
  94.     }  
  95.   
  96.     public boolean getisFirst() {  
  97.         return sp.getBoolean("isFirst"true);  
  98.     }  
  99. }  


第二种方法是更加简单方便:取值时只用传入context和对应的key,就能获取到对应的value;设值时,也是传入context和对应key和value即可,非常方便实用。

[java]  view plain  copy
  1. public class PreferenceUtils {  
  2.     public static String getPrefString(Context context, String key,  
  3.             final String defaultValue) {  
  4.         final SharedPreferences settings = PreferenceManager  
  5.                 .getDefaultSharedPreferences(context);  
  6.         return settings.getString(key, defaultValue);  
  7.     }  
  8.   
  9.     public static void setPrefString(Context context, final String key,  
  10.             final String value) {  
  11.         final SharedPreferences settings = PreferenceManager  
  12.                 .getDefaultSharedPreferences(context);  
  13.         settings.edit().putString(key, value).commit();  
  14.     }  
  15.   
  16.     public static boolean getPrefBoolean(Context context, final String key,  
  17.             final boolean defaultValue) {  
  18.         final SharedPreferences settings = PreferenceManager  
  19.                 .getDefaultSharedPreferences(context);  
  20.         return settings.getBoolean(key, defaultValue);  
  21.     }  
  22.   
  23.     public static boolean hasKey(Context context, final String key) {  
  24.         return PreferenceManager.getDefaultSharedPreferences(context).contains(  
  25.                 key);  
  26.     }  
  27.   
  28.     public static void setPrefBoolean(Context context, final String key,  
  29.             final boolean value) {  
  30.         final SharedPreferences settings = PreferenceManager  
  31.                 .getDefaultSharedPreferences(context);  
  32.         settings.edit().putBoolean(key, value).commit();  
  33.     }  
  34.   
  35.     public static void setPrefInt(Context context, final String key,  
  36.             final int value) {  
  37.         final SharedPreferences settings = PreferenceManager  
  38.                 .getDefaultSharedPreferences(context);  
  39.         settings.edit().putInt(key, value).commit();  
  40.     }  
  41.   
  42.     public static int getPrefInt(Context context, final String key,  
  43.             final int defaultValue) {  
  44.         final SharedPreferences settings = PreferenceManager  
  45.                 .getDefaultSharedPreferences(context);  
  46.         return settings.getInt(key, defaultValue);  
  47.     }  
  48.   
  49.     public static void setPrefFloat(Context context, final String key,  
  50.             final float value) {  
  51.         final SharedPreferences settings = PreferenceManager  
  52.                 .getDefaultSharedPreferences(context);  
  53.         settings.edit().putFloat(key, value).commit();  
  54.     }  
  55.   
  56.     public static float getPrefFloat(Context context, final String key,  
  57.             final float defaultValue) {  
  58.         final SharedPreferences settings = PreferenceManager  
  59.                 .getDefaultSharedPreferences(context);  
  60.         return settings.getFloat(key, defaultValue);  
  61.     }  
  62.   
  63.     public static void setSettingLong(Context context, final String key,  
  64.             final long value) {  
  65.         final SharedPreferences settings = PreferenceManager  
  66.                 .getDefaultSharedPreferences(context);  
  67.         settings.edit().putLong(key, value).commit();  
  68.     }  
  69.   
  70.     public static long getPrefLong(Context context, final String key,  
  71.             final long defaultValue) {  
  72.         final SharedPreferences settings = PreferenceManager  
  73.                 .getDefaultSharedPreferences(context);  
  74.         return settings.getLong(key, defaultValue);  
  75.     }  
  76.   
  77.     public static void clearPreference(Context context,  
  78.             final SharedPreferences p) {  
  79.         final Editor editor = p.edit();  
  80.         editor.clear();  
  81.         editor.commit();  
  82.     }  
  83. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值