6、通用类Utils的常用函数
来源:互联网 发布:生死狙击球棒数据变异 编辑:程序博客网 时间:2024/05/10 21:15
packagecom.example.wechat.common;
importandroid.content.Context;
importandroid.content.SharedPreferences;
importandroid.preference.PreferenceManager;
importandroid.util.Log;
/**
* Created by sing on 2016/1/26.
*/
public classUtils {
/***************************************************************************/
private static final SharedPreferences getSharedPreference(Context context) {
returnPreferenceManager.getDefaultSharedPreferences(context);
}
/**
* 移除SharedPreference
*
*@paramcontext
*@paramkey
*/
public static final void RemoveValue(Context context, String key) {
SharedPreferences.Editor editor =getSharedPreference(context).edit();
editor.remove(key);
booleanresult = editor.commit();
if(!result) {
Log.e("移除Shared","save "+ key +" failed");
}
}
/**
* 获取SharedPreference 值
*
*@paramcontext
*@paramkey
*@return
*/
public static final String getValue(Context context, String key) {
returngetSharedPreference(context).getString(key,"");
}
public static finalBoolean getBooleanValue(Context context, String key) {
returngetSharedPreference(context).getBoolean(key,false);
}
public static final voidputBooleanValue(Context context, String key,
booleanbl) {
SharedPreferences.Editor edit =getSharedPreference(context).edit();
edit.putBoolean(key, bl);
edit.commit();
}
public static final intgetIntValue(Context context, String key) {
returngetSharedPreference(context).getInt(key,0);
}
public static final longgetLongValue(Context context, String key,
longdefault_data) {
returngetSharedPreference(context).getLong(key, default_data);
}
public static final booleanputLongValue(Context context, String key,
Long value) {
SharedPreferences.Editor editor =getSharedPreference(context).edit();
editor.putLong(key, value);
returneditor.commit();
}
public static finalBoolean hasValue(Context context, String key) {
returngetSharedPreference(context).contains(key);
}
/**
* 设置SharedPreference 值
*
*@paramcontext
*@paramkey
*@paramvalue
*/
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);
booleanresult = editor.commit();
if(!result) {
return false;
}
return true;
}
/**
* 设置SharedPreference 值
*
*@paramcontext
*@paramkey
*@paramvalue
*/
public static final boolean putIntValue(Context context, String key,
intvalue) {
SharedPreferences.Editor editor =getSharedPreference(context).edit();
editor.putInt(key, value);
booleanresult = editor.commit();
if(!result) {
return false;
}
return true;
}
/***************************************************************************/
}
importandroid.content.Context;
importandroid.content.SharedPreferences;
importandroid.preference.PreferenceManager;
importandroid.util.Log;
/**
* Created by sing on 2016/1/26.
*/
public classUtils {
/***************************************************************************/
private static final SharedPreferences getSharedPreference(Context context) {
returnPreferenceManager.getDefaultSharedPreferences(context);
}
/**
* 移除SharedPreference
*
*@paramcontext
*@paramkey
*/
public static final void RemoveValue(Context context, String key) {
SharedPreferences.Editor editor =getSharedPreference(context).edit();
editor.remove(key);
booleanresult = editor.commit();
if(!result) {
Log.e("移除Shared","save "+ key +" failed");
}
}
/**
* 获取SharedPreference 值
*
*@paramcontext
*@paramkey
*@return
*/
public static final String getValue(Context context, String key) {
returngetSharedPreference(context).getString(key,"");
}
public static finalBoolean getBooleanValue(Context context, String key) {
returngetSharedPreference(context).getBoolean(key,false);
}
public static final voidputBooleanValue(Context context, String key,
booleanbl) {
SharedPreferences.Editor edit =getSharedPreference(context).edit();
edit.putBoolean(key, bl);
edit.commit();
}
public static final intgetIntValue(Context context, String key) {
returngetSharedPreference(context).getInt(key,0);
}
public static final longgetLongValue(Context context, String key,
longdefault_data) {
returngetSharedPreference(context).getLong(key, default_data);
}
public static final booleanputLongValue(Context context, String key,
Long value) {
SharedPreferences.Editor editor =getSharedPreference(context).edit();
editor.putLong(key, value);
returneditor.commit();
}
public static finalBoolean hasValue(Context context, String key) {
returngetSharedPreference(context).contains(key);
}
/**
* 设置SharedPreference 值
*
*@paramcontext
*@paramkey
*@paramvalue
*/
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);
booleanresult = editor.commit();
if(!result) {
return false;
}
return true;
}
/**
* 设置SharedPreference 值
*
*@paramcontext
*@paramkey
*@paramvalue
*/
public static final boolean putIntValue(Context context, String key,
intvalue) {
SharedPreferences.Editor editor =getSharedPreference(context).edit();
editor.putInt(key, value);
booleanresult = editor.commit();
if(!result) {
return false;
}
return true;
}
/***************************************************************************/
}
0 0
- 6、通用类Utils的常用函数
- 记录一些常用的utils方法6
- utils部分--一些通用的工具类封装
- java常用utils类
- js常用的通用函数
- 常用的java Utils总结
- java常用utils工具类
- Android:常用Utils工具类
- Android:常用Utils工具类
- android源码里那些常用的Utils工具类
- Utils(上)一些常用的工具类
- Utils(下)一些常用的工具类
- 【造轮子】utils工具类(常用方法的封装)
- Oracle 常用的几个通用函数
- 记录一些常用的utils方法
- 记录一些常用的utils方法3
- 记录一些常用的utils方法2
- 记录一些常用的utils方法4
- eclipse插件项目中的后缀名为.exsd文件的作用
- lintcode:Wood Cut
- 使用UICollectionView 重写UICollectionViewLayout实现瀑布流
- C++索引运算符【】和输出运算符<<重载
- 堆的最大申请数量——我能malloc多少?
- 6、通用类Utils的常用函数
- android之播放视频
- postgresql to_char数字转换
- UART中的硬件流控RTS与CTS
- poj 2965 The Pilots Brothers' refrigerator【枚举】
- MyEclipse 项目部署到tomcat时出现错误之——deploy______http://yang8820.iteye.com/blog/1654466
- EmptyResult类型
- Nova API microversion 变更记录 (持续更新)
- 模拟一个trim方法,去除字符串两端的空格