13_数据存储(三): SharedPreferences的工具类UtilsSharedPreferences

Android基础汇总​​​​​​​
package com.aft.yeniu.utils;
 
import android.content.Context;
 
/**
 * 把小型的数据存在SharedPref里面
 *
 * @author xieyalong
 */
public class UtilsSharedPreferences {
 
//SharedPreferences的文件名
 public final static String user = "user";
 public final static String DATE = "date";
 
  //可以将要存储的字段名设置为该类的静态常量,以防出现忘记是哪个表,什么意思,字段混淆,重复定义和存储,如
 
public final static String USER_USERNAME ="userName";//user表的userName字段
 /**
  * 存储数据(Long) sared_prefs:文件名称
  */
 public static void putLongValue(String sared_prefs, Context context,
   String key, long value) {
  context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE).edit()
    .putLong(key, value).commit();
 }
 
 /**
  * 存储数据(Int)
  */
 public static void putIntValue(String sared_prefs, Context context,
   String key, int value) {
  context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE).edit()
    .putInt(key, value).commit();
 }
 
 /**
  * 存储数据(String)
  */
 public static void putStringValue(String sared_prefs, Context context,
   String key, String value) {
  context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE).edit()
    .putString(key, value).commit();
 }
 
 /**
  * 存储数据(boolean)
  */
 public static void putBooleanValue(String sared_prefs, Context context,
   String key, boolean value) {
  context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE).edit()
    .putBoolean(key, value).commit();
 }
 
 /**
  * 取出数据(Long)
  */
 public static long getLongValue(String sared_prefs, Context context,
   String key) {
  return context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE)
    .getLong(key, 0);
 }
 
 /**
  * 取出数据(int)
  */
 public static int getIntValue(String sared_prefs, Context context,
   String key) {
  return context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE)
    .getInt(key, 0);
 }
 
 /**
  * 取出数据(boolean)
  */
 public static boolean getBooleanValue(String sared_prefs, Context context,
   String key, boolean defValue) {
  return context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE)
    .getBoolean(key, defValue);
 }
 
 /**
  * 取出数据(String)
  */
 public static String getStringValue(String sared_prefs, Context context,
   String key) {
  return context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE)
    .getString(key, "");
 }
 
 /**
  * 取出数据(String)
  */
 public static String getStringValue(String sared_prefs, Context context,
   String key, String defult) {
  return context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE)
    .getString(key, "");
 }
 
 /**
  * 清空所有数据
  */
 public static void clear(String sared_prefs, Context context) {
  context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE).edit()
    .clear().commit();
 }
 
 /**
  * 移除指定数据
  */
 public static void remove(String sared_prefs, Context context, String key) {
  context.getSharedPreferences(sared_prefs, Context.MODE_PRIVATE).edit()
    .remove(key).commit();
 }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值