SharedPreferences的工具类
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);
} }
SharedPreferences的工具类的更多相关文章
- Android 分享一个SharedPreferences的工具类,方便保存数据
我们平常保存一些数据,都会用到SharedPreferences,他是保存在手机里面的,具体路径是data/data/你的包名/shared_prefs/保存的文件名.xml, SharedPrefe ...
- SharedPreferences的工具类,使用起来方便、快捷
SharedPreferences的工具类,使用起来方便.快捷:上代码:import android.content.Context;import android.content.SharedPref ...
- 写了个SharedPreferences的工具类(带加密)
/* * Copyright (C) 2014 Jason Fang ( ijasonfang@gmail.com ) * * Licensed under the Apache License, V ...
- Android修改默认SharedPreferences文件的路径,SharedPreferences常用工具类
import android.app.Activity; import android.content.Context; import android.content.ContextWrapper; ...
- Android之SharedPreferences两个工具类
相信Android的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数 ...
- Android 中替代 sharedpreferences 工具类的实现
Android 中替代 sharedpreferences 工具类的实现 背景 想必大家一定用过 sharedpreferences 吧!就我个人而言,特别讨厌每次 put 完数据还要 commit. ...
- java工具类
1.HttpUtilsHttp网络工具类,主要包括httpGet.httpPost以及http参数相关方法,以httpGet为例:static HttpResponse httpGet(HttpReq ...
- Android开发常用工具类
来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...
- SharePreference 工具类封装
import java.util.List;import java.util.Map;import java.util.Set;import com.alibaba.fastjson.JSON;imp ...
随机推荐
- jetty启动https
<Configure id="Server" class="org.eclipse.jetty.server.Server"> <!-- if ...
- Design Pattern - Strategy
Strategy Pattern: The Strategy Pattern defines a family of algorithms,encapsulates each one,and ...
- angular.js升序降序过滤器
向指令添加过滤器 过滤器可以通过一个管道字符(|)和一个过滤器添加到指令中. orderBy 过滤器根据表达式排列数组: 例如: <div ng-app="myApp" ng ...
- 使用MyEclipse构建MAVEN项目
这里用的是MyEclpise的自带的MAVEN插件.Maven最好配置成你自己安装的那个,MyEclipse自带会有些许Bug.用nexus代理Maven的中央仓库,setting.xml的配置文件修 ...
- 判断字符串是否为UTF8编码
UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码.由Ken Thompson于1992年创建.现在已经标准化为RFC 36 ...
- linux5.8安装oracle10g过程记录,换实例一定要改profile的配置
查看系统位数: [root@oracle /]# uname -aLinux oracle 2.6.18-308.el5 #1 SMP Fri Jan 27 17:21:15 EST 2012 i68 ...
- Linux的文件/目录的权限
文件类型 权限 属主 属组 目录名/文件名drwx------ 2 alice girls 4096 9月 25 11:33 alicedrwx------ 2 rose girls 4096 9月 ...
- Java Method Logging with AOP and Annotations
Sometimes, I want to log (through slf4j and log4j) every execution of a method, seeing what argument ...
- CSS中RGBA的兼容方法以及透明度计算方法
CSS对IE使用背景透明 实现 rgba 效果 height:35px; background: -webkit-gradient(linear,left top,left bottom,from(r ...
- EconomicIndoor集成测试
加密时序列号相同引发的呼叫功能异常 现象描述: 配置完房间号一次性呼叫问题 问题描述: 两台室内机升级后配置到同一门口机, 各种配置正确. 作为主叫呼不出去, 作为被叫可以接听, 每修改一次房间号, ...