Android_应用层_SharedPreferences单例快速存储工具
Android_应用层_SharedPreferences单例快速存储工具。
package com.huawei.framework.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.huawei.se.BaseApplication;
//@author zhanglong
public final class SharedPreferencesUtil {
private static SharedPreferencesUtil instance;
private SharedPreferences sharedPreferences = null;
private SharedPreferencesUtil() {
initSharedPreferences(BaseApplication.getAppContext());
}
public static SharedPreferencesUtil getInstance() {
if (null == instance) {
instance = new SharedPreferencesUtil();
}
return instance;
}
public void initSharedPreferences(Context context) {
sharedPreferences = context.getSharedPreferences("GCSharedPreferences", Context.MODE_MULTI_PROCESS);
}
public SharedPreferences getSharedPreferences() {
return sharedPreferences;
}
/**
* 保存键值
*
* @param key
* @param value
*/
public void saveStringValue(String key, String value) {
if (sharedPreferences == null) {
return;
}
Editor edit = sharedPreferences.edit();
edit.putString(key, value);
edit.commit();
}
/**
* 获取String值
*
* @param key
* @param defaultValue
* @return
*/
public String readStringValue(String key, String defaultValue) {
if (sharedPreferences == null) {
return defaultValue;
}
return sharedPreferences.getString(key, defaultValue);
}
/**
* 保存键值 Boolean型
*
* @param key
* @param value
*/
public void saveBooleanValue(String key, boolean value) {
if (sharedPreferences == null) {
return;
}
Editor edit = sharedPreferences.edit();
edit.putBoolean(key, value);
edit.commit();
}
/**
* 获取boolean值
*
* @param key
* @param defaultValue
* @return
*/
public boolean readBooleanValue(String key, boolean defaultValue) {
if (sharedPreferences == null) {
return defaultValue;
}
return sharedPreferences.getBoolean(key, defaultValue);
}
/**
* 保存键值 Int
*
* @param key
* @param value
*/
public void saveIntValue(String key, int value) {
if (sharedPreferences == null) {
return;
}
Editor edit = sharedPreferences.edit();
edit.putInt(key, value);
edit.commit();
}
/**
* 获取float值
*
* @param key
* @param defaultValue
* @return
*/
public int readIntValue(String key, int defaultValue) {
if (sharedPreferences == null) {
return defaultValue;
}
return sharedPreferences.getInt(key, defaultValue);
}
/**
* 保存键值 Float
*
* @param key
* @param value
*/
public void saveFloatValue(String key, Float value) {
if (sharedPreferences == null) {
return;
}
Editor edit = sharedPreferences.edit();
edit.putFloat(key, value);
edit.commit();
}
/**
* 获取Float值
*
* @param key
* @param defaultValue
* @return
*/
public Float readFloatValue(String key, Float defaultValue) {
if (sharedPreferences == null) {
return defaultValue;
}
return sharedPreferences.getFloat(key, defaultValue);
}
/**
* 保存键值 Long
*
* @param key
* @param value
*/
public void saveLongValue(String key, Long value) {
if (sharedPreferences == null) {
return;
}
Editor edit = sharedPreferences.edit();
edit.putLong(key, value);
edit.commit();
}
/**
* 获取Long值
*
* @param key
* @param defaultValue
* @return
*/
public Long readLongValue(String key, Long defaultValue) {
if (sharedPreferences == null) {
return defaultValue;
}
return sharedPreferences.getLong(key, defaultValue);
}
/**
* 删除键值
*
* @param key
*
**/
public void deleteValue(String key) {
if (sharedPreferences.contains(key)) {
Editor edit = sharedPreferences.edit();
edit.remove(key);
edit.commit();
}
}
}
单例SP:
1能使存储数据是更加的快速和方便,节约资源;
2、采用BaseApplication.getAppContext()避免SP引用Activity的context,导致内存泄漏,全局的Context生命周期长,随着主应用进程的结束才释放。