編輯:關於android開發
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));
}
}
}
Android 繪圖機制:canvas初解 Canvas 即“畫布”的意思,在Android中用其來進行2D繪畫。 在使用can
集成websocket即時通訊 java聊天源碼 代碼下載 java後台框架源碼 websocket源碼 IM,websocket即時通訊獲取【下載地址】 &
階段一:一個簡單的天氣預報應用的完整實現過程(二),階段天氣預報“階段一”是指我第一次系統地學習Android開發。這主要是對我的學習過程作個記錄
實用控件分享:自定義逼真相機光圈View,控件相機光圈view最近手機界開始流行雙攝像頭,大光圈功能也應用而生。所謂大光圈功能就是能夠對照片進行後期重新對焦,其實現的原理