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);
} }

最新文章

  1. redis+cookies实现session机制(解决 手机浏览器不自动回传cookies导致session不可用问题)
  2. [AlwaysOn Availability Groups]AlwaysOn健康诊断日志
  3. PE530 : GCD of Divisors
  4. EasyUI笔记
  5. Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree
  6. Xamarin Anroid开发教程之下载安装Xamarin
  7. php字符串常用函数
  8. JS日期格式化(网上转载)
  9. php Ajax 局部刷新
  10. 解决open-vm-tools安装时Failed to get unit file state for run-vmblockx2dfuse.mount
  11. ZOJ 1108 & HDU 1160 - FatMouse's Speed
  12. React ref回调函数例子
  13. python面向对象三大特性之封装
  14. Laravel-2
  15. redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)
  16. prototype.js的Ajax对IE8兼容问题解决方案
  17. 20135231 JAVA实验报告三:敏捷开发与XP实践
  18. linux编程合并多个静态库.a为一个.a
  19. 【LOJ10121】与众不同
  20. linux 内核态调试函数BUG_ON()[转]

热门文章

  1. js获取计算的样式(非行间样式)
  2. Debian系Linux的dpkg命令
  3. 分布式版本控制系统Git-----2.上传至远程仓库之基础版
  4. 云锁Linux服务器安全软件安装及防护webshell、CC、XSS跨站攻击设置
  5. gitignore git提交忽略文件
  6. Java Method Logging with AOP and Annotations
  7. hdu1002
  8. 明天学习一下验证码的匹配和thinkphp第十三章
  9. jQuery第三章
  10. JavaScript的内置对象(Date日期+string字符串)基础语法总结