Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> SharePreference 工具類封裝,sharepreference封裝

SharePreference 工具類封裝,sharepreference封裝

編輯:關於android開發

SharePreference 工具類封裝,sharepreference封裝


import java.util.List;
import java.util.Map;
import java.util.Set;
import com.alibaba.fastjson.JSON;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build;
import android.text.TextUtils;

/**
* @ClassName: SharePreferenceUtils
* @Description: SharePreference工具類
* @Author:lxs
*
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressWarnings("unchecked")
public class SharePreferenceTools {

private SharedPreferences sharedPreferences;
private Editor editor;
private final int ZERO = 0;

public SharePreferenceTools(Context context) {
this(context, ConfigUtils.PREFERENCE_DEFAULT_NAME, Context.MODE_PRIVATE);
}

public SharePreferenceTools(Context context, String preferenceName) {
this(context, preferenceName, Context.MODE_PRIVATE);
}

@SuppressLint("CommitPrefEdits")
public SharePreferenceTools(Context context, String preferenceName, int mode) {
sharedPreferences = context.getSharedPreferences(preferenceName, mode);
editor = sharedPreferences.edit();
}

public void putString(String key, String value) {
if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
editor.putString(key, value);
editor.commit();
}
}

public String getString(String key) {
String value = null;
if (!TextUtils.isEmpty(key)) {
value = sharedPreferences.getString(key, null);
}
return value;
}

public String getString(String key, String defaultValue) {
String value = null;
if (!TextUtils.isEmpty(key)) {
value = sharedPreferences.getString(key, defaultValue);
}
return value;
}

public void putInt(String key, int value) {
if (!TextUtils.isEmpty(key)) {
editor.putInt(key, value);
editor.commit();
}
}

public int getInt(String key) {
int value = ZERO;
if (!TextUtils.isEmpty(key)) {
value = sharedPreferences.getInt(key, ZERO);
}
return value;
}

public int getInt(String key, int defaultValue) {
int value = defaultValue;
if (!TextUtils.isEmpty(key)) {
value = sharedPreferences.getInt(key, defaultValue);
}
return value;
}

public void putBoolean(String key, boolean value) {
if (!TextUtils.isEmpty(key)) {
editor.putBoolean(key, value);
editor.commit();
}
}

public boolean getBoolean(String key) {
boolean result = false;
if (!TextUtils.isEmpty(key)) {
result = sharedPreferences.getBoolean(key, false);
}
return result;
}

public boolean getBoolean(String key, boolean defaultValue) {
boolean result = defaultValue;
if (!TextUtils.isEmpty(key)) {
result = sharedPreferences.getBoolean(key, defaultValue);
}
return result;
}

public void putLong(String key, long value) {
if (!TextUtils.isEmpty(key)) {
editor.putLong(key, value);
editor.commit();
}
}

public long getLong(String key) {
long value = ZERO;
if (!TextUtils.isEmpty(key)) {
value = sharedPreferences.getLong(key, ZERO);
}
return value;
}

public long getLong(String key, long defaultValue) {
long value = defaultValue;
if (!TextUtils.isEmpty(key)) {
value = sharedPreferences.getLong(key, defaultValue);
}
return value;
}

public void putFloat(String key, float value) {
if (!TextUtils.isEmpty(key)) {
editor.putFloat(key, value);
editor.commit();
}
}

public float getFloat(String key) {
float value = ZERO;
if (!TextUtils.isEmpty(key)) {
value = sharedPreferences.getFloat(key, ZERO);
}
return value;
}

public float getFloat(String key, float defaultValue) {
float value = defaultValue;
if (!TextUtils.isEmpty(key)) {
value = sharedPreferences.getFloat(key, defaultValue);
}
return value;
}

public void putStringSet(String key, Set<String> value) {
if (!TextUtils.isEmpty(key)) {
editor.putStringSet(key, value);
editor.commit();
}
}

public Set<String> getStringSet(String key) {
Set<String> set = null;
if (!TextUtils.isEmpty(key)) {
set = sharedPreferences.getStringSet(key, null);
}
return set;
}

public Set<String> getStringSet(String key, Set<String> defaultValue) {
Set<String> set = defaultValue;
if (!TextUtils.isEmpty(key)) {
set = sharedPreferences.getStringSet(key, defaultValue);
}
return set;
}

/**
* 添加對象
*
* @param key
* @param t
*/
public <T> void putModel(String key, T t) {
if (!TextUtils.isEmpty(key) && t != null) {
putString(key, JSON.toJSONString(t));// fastjson
}
}

/**
* 獲取對象
*
* @param key
* @param t
* @return
*/
public <T> T getModel(String key, Class<T> clazz) {
String value = null;
if (!TextUtils.isEmpty(key)) {
value = getString(key);
}
return TextUtils.isEmpty(value) ? null : JSON.parseObject(value, clazz);// fastjson
}

/**
* 添加集合
*
* @param key
* @param t
*/
public <T> void putModels(String key, List<T> t) {
if (!TextUtils.isEmpty(key) && t != null && t.size() > ZERO) {
putString(key, JSON.toJSONString(t));// fastjson
}
}

/**
* 獲取集合
*
* @param key
* @param t
* @return
*/
public <T> List<T> getModels(String key, Class<T> clazz) {
String value = null;
if (!TextUtils.isEmpty(key)) {
value = getString(key);
}
return TextUtils.isEmpty(value) ? null : JSON.parseArray(value, clazz);// fastjson
}

/**
* 查詢某個key是否已經存在
*
* @param key
* @return
*/
public boolean contains(String key) {
return sharedPreferences.contains(key);
}

/**
* 移除某個key值已經對應的值
*
* @param key
*/
public void remove(String key) {
editor.remove(key);
editor.commit();
}

/**
* 清除所有數據
*/
public void clear() {
editor.clear();
editor.commit();
}

/**
* 返回所有的鍵值對
*
* @return
*/
public Map<String, ?> getAll() {
return sharedPreferences.getAll();
}

/**
* 日志輸出所有鍵值對
*/
public void selectKeyAll() {
Map<String, Object> map = (Map<String, Object>) sharedPreferences.getAll();
for (String key : map.keySet()) {
Print.d("key= " + key + " and value= " + map.get(key));
}
}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved