关键字
SharedPreference
通用系列
数据存储
摘要:
主要内容:
本文介绍了封装后的SharedPreference,以便快速开发;
只保留必要的定制,重复的代码封装到一个通用类中;
说明
- 具体用法参照
UspDemo.java
文件; - 通用源文件为
Usp.java
代码
【用法】
//: TestUsp.java
String KEY_FIRST = "FIRST";
Ulog.d("Before First:"+Usp.init(this).getInt(KEY_FIRST, 22));
Usp.init(this).putInt(KEY_FIRST, 101).commit();
Ulog.d("After First:"+Usp.init(this).getInt(KEY_FIRST, 23));
//~ first time you get: 22 and 101; second time you get: 101 and 101;
【通用源码】
var2) {
sEditor.putStringSet(var1, var2).apply();
return INSTANCE;
}
public Usp putInt(String var1, int var2) {
sEditor.putInt(var1, var2).apply();
return INSTANCE;
}
public Usp putLong(String var1, long var2) {
sEditor.putLong(var1, var2).apply();
return INSTANCE;
}
public Usp putFloat(String var1, float var2) {
sEditor.putFloat(var1, var2).apply();
return INSTANCE;
}
public Usp putBoolean(String var1, boolean var2) {
sEditor.putBoolean(var1, var2).apply();
return INSTANCE;
}
public Usp remove(String var1) {
sEditor.remove(var1).apply();
return INSTANCE;
}
public String getString(String var1, String var2) {
return INSTANCE.mSharedPreferences.getString(var1, var2);
}
public Set
getStringSet(String var1, Set
var2) {
return INSTANCE.mSharedPreferences.getStringSet(var1, var2);
}
public int getInt(String var1, int var2) {
return INSTANCE.mSharedPreferences.getInt(var1, var2);
}
public long getLong(String var1, long var2) {
return INSTANCE.mSharedPreferences.getLong(var1, var2);
}
public float getFloat(String var1, float var2) {
return INSTANCE.mSharedPreferences.getFloat(var1, var2);
}
public boolean getBoolean(String var1, boolean var2) {
return INSTANCE.mSharedPreferences.getBoolean(var1, var2);
}
public boolean contains(String var1) {
return INSTANCE.mSharedPreferences.contains(var1);
}
///
// Self
///
private final SharedPreferences mSharedPreferences;
private Usp(Context context, @Nullable String spName) {
if (TextUtils.isEmpty(spName)) {
// 默认的包名使用「包名_SP」
spName = context.getApplicationContext().getPackageName() + "_SP";
}
mSharedPreferences = context.getApplicationContext().getSharedPreferences(spName, Context.MODE_PRIVATE);
}
}
" data-snippet-id="ext.8d6bdb4c6f0345f187b58078dc5fced8" data-snippet-saved="false" data-codota-status="done">
//: Usp.java
package com.lyloou.lou.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import java.util.Set;
/**
* 类描述:SharedPreference 工具类;
* 创建人: Lou
* 创建时间: 2016/7/7 14:38
* 修改人: Lou
* 修改时间:2016/7/7 14:38
* 修改备注:
*/
public final class Usp {
///////////////////////////////////////////////////////////////////////////
// 对外提供接口
///////////////////////////////////////////////////////////////////////////
private static Usp INSTANCE;
private static SharedPreferences.Editor sEditor;
public static Usp init(Context context, String spName) {
if (INSTANCE == null)
INSTANCE = new Usp(context, spName);
if (sEditor == null)
sEditor = INSTANCE.mSharedPreferences.edit();
return INSTANCE;
}
public static Usp init(Context context) {
return init(context, null);
}
public void commit() {
sEditor.commit();
}
public Usp clear() {
sEditor.clear().apply();
return INSTANCE;
}
public Usp putString(String var1, String var2) {
sEditor.putString(var1, var2).apply();
return INSTANCE;
}
public Usp putString(String var1, Set<String> var2) {
sEditor.putStringSet(var1, var2).apply();
return INSTANCE;
}
public Usp putInt(String var1, int var2) {
sEditor.putInt(var1, var2).apply();
return INSTANCE;
}
public Usp putLong(String var1, long var2) {
sEditor.putLong(var1, var2).apply();
return INSTANCE;
}
public Usp putFloat(String var1, float var2) {
sEditor.putFloat(var1, var2).apply();
return INSTANCE;
}
public Usp putBoolean(String var1, boolean var2) {
sEditor.putBoolean(var1, var2).apply();
return INSTANCE;
}
public Usp remove(String var1) {
sEditor.remove(var1).apply();
return INSTANCE;
}
public String getString(String var1, String var2) {
return INSTANCE.mSharedPreferences.getString(var1, var2);
}
public Set<String> getStringSet(String var1, Set<String> var2) {
return INSTANCE.mSharedPreferences.getStringSet(var1, var2);
}
public int getInt(String var1, int var2) {
return INSTANCE.mSharedPreferences.getInt(var1, var2);
}
public long getLong(String var1, long var2) {
return INSTANCE.mSharedPreferences.getLong(var1, var2);
}
public float getFloat(String var1, float var2) {
return INSTANCE.mSharedPreferences.getFloat(var1, var2);
}
public boolean getBoolean(String var1, boolean var2) {
return INSTANCE.mSharedPreferences.getBoolean(var1, var2);
}
public boolean contains(String var1) {
return INSTANCE.mSharedPreferences.contains(var1);
}
///////////////////////////////////////////////////////////////////////////
// Self
///////////////////////////////////////////////////////////////////////////
private final SharedPreferences mSharedPreferences;
private Usp(Context context, @Nullable String spName) {
if (TextUtils.isEmpty(spName)) {
// 默认的包名使用「包名_SP」
spName = context.getApplicationContext().getPackageName() + "_SP";
}
mSharedPreferences = context.getApplicationContext().getSharedPreferences(spName, Context.MODE_PRIVATE);
}
}