Handler优化以及SharedPreferences工具类的封装
来源:互联网 发布:淘宝假货申诉凭证制造 编辑:程序博客网 时间:2024/06/06 02:48
下面直接上一段优化后的Handler,由于能力有限,如有问题可以与本菜鸟联系 QQ:2428566234
技术讨论群:387648673
public class SplashActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
SplashHandler handler = new SplashHandler(this);
handler.sendEmptyMessageDelayed(100, 1000);
}
/* Handler是内部类 内部类存在对外部类的持有
*当外部类声明周期结束时如果不静态持有会一直存在,会造成内存泄漏 所以要静态
* Android 源码内部类几乎都是静态的*/
private static class SplashHandler extends Handler {
private final WeakReference<SplashActivity> splash;
public SplashHandler(SplashActivity activity) {
splash = new WeakReference<SplashActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
SplashActivity activity = splash.get();
if (activity != null) {
if(!SharedPreferencesUtil.getBoolean(activity,"isFirstCome")) {
Intent intent = new Intent(activity,WelcomeActivity.class);
activity.startActivity(intent);
activity.finish();
} else {
Intent intent = new Intent(activity,MainActivity.class);
activity.startActivity(intent);
activity.finish();
}
}
super.handleMessage(msg);
}
}
下面是SharedPreferences的工具类
public class SharedPreferencesUtil {
public static boolean getBoolean(Context context, String key) {
return getSharedPreferences(context).getBoolean(key, false);
}
public static boolean getBoolean(Context context, String prefName, String prefKey,
boolean defaultValue) {
return getSharedPreferences(context, prefName).getBoolean(prefKey, defaultValue);
}
public static void putBoolean(Context context, String key, boolean b) {
getSharedPreferences(context).edit().putBoolean(key, b).commit();
}
public static void putBoolean(Context context, String prefName, String prefKey, boolean value) {
getSharedPreferences(context, prefName).edit().putBoolean(prefKey, value).commit();
}
public static String getString(Context context, String key) {
return getSharedPreferences(context).getString(key, "");
}
private static String getString(Context context, String key, String defaultValue) {
return getSharedPreferences(context).getString(key, defaultValue);
}
public static String getString(Context context, String prefName, String prefKey,
String defaultValue) {
return getSharedPreferences(context, prefName).getString(prefKey, defaultValue);
}
public static void putString(Context context, String key, String value) {
getSharedPreferences(context).edit().putString(key, value).commit();
}
public static void putString(Context context, String prefName, String prefKey, String value) {
getSharedPreferences(context, prefName).edit().putString(prefKey, value).commit();
}
public static long getLong(Context context, String key) {
return getSharedPreferences(context).getLong(key, -1L);
}
public static long getLong(Context context, String key, long defalutValue) {
return getSharedPreferences(context).getLong(key, defalutValue);
}
public static long getLong(Context context, String prefName, String prefKey, long defaultValue) {
return getSharedPreferences(context, prefName).getLong(prefKey, defaultValue);
}
public static void putLong(Context context, String key, long value) {
getSharedPreferences(context).edit().putLong(key, value).commit();
}
public static void putLong(Context context, String prefName, String prefKey, long value) {
getSharedPreferences(context, prefName).edit().putLong(prefKey, value).commit();
}
public static int getInt(Context context, String key) {
return getSharedPreferences(context).getInt(key, -1);
}
public static int getInt(Context context, String key, int defaultValue) {
return getSharedPreferences(context).getInt(key, defaultValue);
}
public static int getInt(Context context, String prefName, String prefKey, int defaultValue) {
return getSharedPreferences(context, prefName).getInt(prefKey, defaultValue);
}
public static void putInt(Context context, String key, int value) {
getSharedPreferences(context).edit().putInt(key, value).commit();
}
public static void putInt(Context context, String prefName, String prefKey, int value) {
getSharedPreferences(context, prefName).edit().putInt(prefKey, value).commit();
}
public static float getFloat(Context context, String prefName, String prefKey,
float defaultValue) {
return getSharedPreferences(context, prefName).getFloat(prefKey, defaultValue);
}
public static void putFloat(Context context, String prefName, String prefKey, float value) {
getSharedPreferences(context, prefName).edit().putFloat(prefKey, value).commit();
}
public static void putFloat(Context context, String prefKey, float value) {
getSharedPreferences(context).edit().putFloat(prefKey, value).commit();
}
/**
* 移除SharedPreferences
*
* @param context
* @param prefKey
*/
public static void remove(Context context, String prefKey) {
getSharedPreferences(context).edit().remove(prefKey).commit();
}
/**
* 清除文件内所有内容
* @param context
* @param preName
*/
public static void clear(Context context, String preName) {
getSharedPreferences(context, preName).edit().clear().commit();
}
/**
* 得到默认SharePreference
*
* @param context
* @return
*/
public static SharedPreferences getSharedPreferences(Context context) {
if (context == null) {
context =AppContext.mContext;
}
return getSharedPreferences(context, null);
}
/**
* 根据名字得到SharePreference
*
* @param context
* @param prefName 独立存储文件
* @return
*/
public static SharedPreferences getSharedPreferences(Context context, String prefName) {
if (TextUtils.isEmpty(prefName)) {
return PreferenceManager.getDefaultSharedPreferences(context);
} else {
return context.getSharedPreferences(prefName, Context.MODE_PRIVATE);
}
}
- Handler优化以及SharedPreferences工具类的封装
- 封装SharedPreferences的工具类
- 工具类的封装--SharedPreferences封装
- SharedPreferences工具类封装
- sharedPreferences工具类封装
- Android封装SharedPreferences工具类
- SharedPreferences util工具类封装
- 优化SharedPreferences的工具类,方便保存数据
- SharedPreferences 的工具类
- SharedPreferences的工具类
- SharedPreferences的工具类
- SharedPreferences的工具类
- JDBC工具类的封装以及运用
- Android SharedPreferences的工具类
- Android SharedPreferences的工具类
- Android SharedPreferences一个很好的封装类
- 自定义简单的封装SharedPreferences缓存类
- 关于SharedPreferences的封装
- C# 中的委托和事件
- javascript-3.数组(1)
- CF 626 B. Cards
- 大数据中的隐藏偏差
- JS DOM编程艺术学习笔记(二)
- Handler优化以及SharedPreferences工具类的封装
- eclipse快捷键以及使用技巧大全 (其中shift+ctrl+F 格式化 会和搜狗输入法冲突,把输入法这个关了)
- 网络:用 socket 做简单聊天
- javascript-4.数组(2)
- 顺时针旋转矩阵90°的几种方法
- 线性回归-3-最小二乘法
- 10分钟-jQuery操作DOM元素
- Linux _ pthread 线程 信号
- 快速排序算法