记录一下sp的封装使用
public class SharedPreferencesUtils {
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private Context context;
private String fileName;
public SharedPreferences getSPreferences() {
return preferences;
}
public static SharedPreferencesUtils getInstance(String fileName) {
return new SharedPreferencesUtils(MainApplication.getContext(), fileName);
}
@SuppressLint({"WorldReadableFiles", "WorldWriteableFiles"})
public SharedPreferencesUtils(Context context, String fileName) {
this.context = context;
this.fileName = fileName;
if (TextUtils.isEmpty(fileName)) {
this.fileName = StringUtils.packageName + "_prefers";
}
try {
preferences = context.getSharedPreferences(fileName, Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
} catch (SecurityException e) {
preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
makeWorldReadable(context, fileName);
}
editor = preferences.edit();
}
@SuppressLint("SetWorldReadable")
public static void makeWorldReadable(Context context, String preferName) {
try {
File sharedPreferDir = new File(context.getApplicationInfo().dataDir, "shared_prefs");
File prefsFile = new File(sharedPreferDir, preferName + ".xml");
prefsFile.setReadable(true, false);
prefsFile.setExecutable(true, false);
} catch (Exception e) {
e.printStackTrace();
}
}
public Object getValue(String key, Object defValue) {
if (preferences == null) {
return defValue;
}
if (defValue instanceof String) {
return preferences.getString(key, (String) defValue);
}
if (defValue instanceof Boolean) {
return preferences.getBoolean(key, (Boolean) defValue);
}
if (defValue instanceof Integer) {
return preferences.getInt(key, (Integer) defValue);
}
if (defValue instanceof Long) {
return preferences.getLong(key, (Long) defValue);
}
if (defValue instanceof Float) {
return preferences.getFloat(key, (Float) defValue);
}
if (defValue instanceof Set<?>) {
return preferences.getStringSet(key, (Set<String>) defValue);
}
return defValue;
}
public boolean setValue(String key, Object defValue) {
if (preferences == null) return false;
if (defValue instanceof String) {
editor.putString(key, (String) defValue);
}
if (defValue instanceof Boolean) {
editor.putBoolean(key, (Boolean) defValue);
}
if (defValue instanceof Integer) {
editor.putInt(key, (Integer) defValue);
}
if (defValue instanceof Long) {
editor.putLong(key, (Long) defValue);
}
if (defValue instanceof Float) {
editor.putFloat(key, (Float) defValue);
}
if (defValue instanceof Set<?>) {
remove(key);
editor.putStringSet(key, (Set<String>) defValue);
}
boolean isCommit = editor.commit();
if (isCommit) {
makeWorldReadable(context, fileName);
}
return isCommit;
}
public Map<String, ?> getAll() {
return preferences.getAll();
}
public boolean clear() {
boolean isCommit = editor.clear().commit();
if (isCommit) {
makeWorldReadable(context, fileName);
}
return isCommit;
}
public boolean remove(String key) {
boolean isCommit = editor.remove(key).commit();
if (isCommit) {
makeWorldReadable(context, fileName);
}
return isCommit;
}
}
用法也很简单,示例如下:
SharedPreferencesUtils preferencesUtils = new SharedPreferencesUtils(context, "config");
//设置键值对
//isCommit为true则保存成功,反之则失败
boolean isCommit = preferencesUtils.setValue("key","value");
//获取键值对
String value = (String) preferencesUtils.getValue("key","默认值");
评论(0)