6、通用类Utils的常用函数
2016-01-26 16:22
295 查看
package
com.example.wechat.common;
import
android.content.Context;
import
android.content.SharedPreferences;
import
android.preference.PreferenceManager;
import
android.util.Log;
/**
* Created by sing on 2016/1/26.
*/
public class
Utils {
/***************************************************************************/
private
static final SharedPreferences getSharedPreference(Context context) {
return
PreferenceManager.getDefaultSharedPreferences(context);
}
/**
* 移除SharedPreference
*
*
@param
context
*
@param
key
*/
public
static final void RemoveValue(Context context, String key) {
SharedPreferences.Editor editor =
getSharedPreference(context).edit();
editor.remove(key);
boolean
result = editor.commit();
if
(!result) {
Log.e("移除Shared",
"save "
+ key +
" failed");
}
}
/**
* 获取SharedPreference 值
*
*
@param
context
*
@param
key
*
@return
*/
public
static final String getValue(Context context, String key) {
return
getSharedPreference(context).getString(key,
"");
}
public static final
Boolean getBooleanValue(Context context, String key) {
return
getSharedPreference(context).getBoolean(key,
false);
}
public static final void
putBooleanValue(Context context, String key,
boolean
bl) {
SharedPreferences.Editor edit =
getSharedPreference(context).edit();
edit.putBoolean(key, bl);
edit.commit();
}
public static final int
getIntValue(Context context, String key) {
return
getSharedPreference(context).getInt(key,
0);
}
public static final long
getLongValue(Context context, String key,
long
default_data) {
return
getSharedPreference(context).getLong(key,
default_data);
}
public static final boolean
putLongValue(Context context, String key,
Long value) {
SharedPreferences.Editor editor =
getSharedPreference(context).edit();
editor.putLong(key, value);
return
editor.commit();
}
public static final
Boolean hasValue(Context context, String key) {
return
getSharedPreference(context).contains(key);
}
/**
* 设置SharedPreference 值
*
*
@param
context
*
@param
key
*
@param
value
*/
public
static final boolean putValue(Context context, String key,
String value) {
value = value ==
null
?
""
: value;
SharedPreferences.Editor editor =
getSharedPreference(context).edit();
editor.putString(key, value);
boolean
result = editor.commit();
if
(!result) {
return false;
}
return true;
}
/**
* 设置SharedPreference 值
*
*
@param
context
*
@param
key
*
@param
value
*/
public
static final boolean putIntValue(Context context, String key,
int
value) {
SharedPreferences.Editor editor =
getSharedPreference(context).edit();
editor.putInt(key, value);
boolean
result = editor.commit();
if
(!result) {
return false;
}
return true;
}
/***************************************************************************/
}
com.example.wechat.common;
import
android.content.Context;
import
android.content.SharedPreferences;
import
android.preference.PreferenceManager;
import
android.util.Log;
/**
* Created by sing on 2016/1/26.
*/
public class
Utils {
/***************************************************************************/
private
static final SharedPreferences getSharedPreference(Context context) {
return
PreferenceManager.getDefaultSharedPreferences(context);
}
/**
* 移除SharedPreference
*
*
@param
context
*
@param
key
*/
public
static final void RemoveValue(Context context, String key) {
SharedPreferences.Editor editor =
getSharedPreference(context).edit();
editor.remove(key);
boolean
result = editor.commit();
if
(!result) {
Log.e("移除Shared",
"save "
+ key +
" failed");
}
}
/**
* 获取SharedPreference 值
*
*
@param
context
*
@param
key
*
@return
*/
public
static final String getValue(Context context, String key) {
return
getSharedPreference(context).getString(key,
"");
}
public static final
Boolean getBooleanValue(Context context, String key) {
return
getSharedPreference(context).getBoolean(key,
false);
}
public static final void
putBooleanValue(Context context, String key,
boolean
bl) {
SharedPreferences.Editor edit =
getSharedPreference(context).edit();
edit.putBoolean(key, bl);
edit.commit();
}
public static final int
getIntValue(Context context, String key) {
return
getSharedPreference(context).getInt(key,
0);
}
public static final long
getLongValue(Context context, String key,
long
default_data) {
return
getSharedPreference(context).getLong(key,
default_data);
}
public static final boolean
putLongValue(Context context, String key,
Long value) {
SharedPreferences.Editor editor =
getSharedPreference(context).edit();
editor.putLong(key, value);
return
editor.commit();
}
public static final
Boolean hasValue(Context context, String key) {
return
getSharedPreference(context).contains(key);
}
/**
* 设置SharedPreference 值
*
*
@param
context
*
@param
key
*
@param
value
*/
public
static final boolean putValue(Context context, String key,
String value) {
value = value ==
null
?
""
: value;
SharedPreferences.Editor editor =
getSharedPreference(context).edit();
editor.putString(key, value);
boolean
result = editor.commit();
if
(!result) {
return false;
}
return true;
}
/**
* 设置SharedPreference 值
*
*
@param
context
*
@param
key
*
@param
value
*/
public
static final boolean putIntValue(Context context, String key,
int
value) {
SharedPreferences.Editor editor =
getSharedPreference(context).edit();
editor.putInt(key, value);
boolean
result = editor.commit();
if
(!result) {
return false;
}
return true;
}
/***************************************************************************/
}
相关文章推荐
- Python3.4 垃圾清理(GUI)
- UART中的硬件流控RTS与CTS
- MyEclipse 项目部署到tomcat时出现错误之——deploy______http://yang8820.iteye.com/blog/1654466
- MySQL5.6:基于GTID的主从复制
- 怎样在UICollectionView中添加Header和footer
- 堆的最大申请数量——我能malloc多少?
- eclipse插件项目中的后缀名为.exsd文件的作用
- Centos(Linux)下用户权限委派配置介绍
- C++索引运算符【】和输出运算符<<重载
- Hbase学习1_CentOS6.5下Hbase1.1.2安装
- 5、登录界面布局文件详解
- lintcode:Wood Cut
- Random类
- android keystore 找回密码
- Aspose Word 导入
- 【从0到1学Web前端】CSS伪类和伪元素
- 数据库的基本操作
- html行级元素与块级元素的比较
- 卡特兰数
- python代码片段——获得三天前时间