有3点厉害的地方
1.使用方便,因为获取了泛型T的class
2.用流处理对象的转化
3.支持List
package com.example.test;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
public abstract class SharedPreferencesUtils<T> {
private Context context;
private Class<T> clx;
private String fileName;
private String fileName = "default";
private int
mode = Context.
MODE_PRIVATE;
public SharedPreferencesUtils(Context context) {
this.
context = context; }
public SharedPreferencesUtils(Context context, Class<
T> clx) {
this.
context = context;
this.
clx = clx; }
public SharedPreferencesUtils setFileName(String fileName) {
this.
fileName = fileName;
return this; }
public void put(String key, String val) { SharedPreferences sp =
context.getSharedPreferences(
fileName,
mode); SharedPreferences.Editor editor = sp.edit(); editor.putString(key, val); editor.apply(); }
public void put(String key,
int val) { SharedPreferences sp =
context.getSharedPreferences(
fileName,
mode); SharedPreferences.Editor editor = sp.edit(); editor.putInt(key, val); editor.apply(); }
public void put(String key,
boolean val) { SharedPreferences sp =
context.getSharedPreferences(
fileName,
mode); SharedPreferences.Editor editor = sp.edit(); editor.putBoolean(key, val); editor.apply(); }
public void put(String key,
float val) { SharedPreferences sp =
context.getSharedPreferences(
fileName,
mode); SharedPreferences.Editor editor = sp.edit(); editor.putFloat(key, val); editor.apply(); }
public void put(String key,
long val) { SharedPreferences sp =
context.getSharedPreferences(
fileName,
mode); SharedPreferences.Editor editor = sp.edit(); editor.putLong(key, val); editor.apply(); }
public void put(String key, Object val) { ByteArrayOutputStream bos =
new ByteArrayOutputStream();
try { ObjectOutputStream os =
new ObjectOutputStream(bos); os.writeObject(val); }
catch (IOException e) { e.printStackTrace(); }
byte[] bytes = bos.toByteArray(); String base64String = Base64.
encodeToString(bytes, Base64.
NO_WRAP); put(key, base64String); }
public
T get(String key) { SharedPreferences sp =
context.getSharedPreferences(
fileName, Context.
MODE_PRIVATE); Class<
T> tClass = (Class<
T>) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[
0];
if (tClass == String.
class) {
return (
T) sp.
getString(key,
""); }
else if (tClass == Integer.
class) {
return (
T)(Integer)sp.getInt(key,
0); }
else if (tClass == Float.
class) {
return (
T)(Float)sp.getFloat(key,
0.0f); }
else if (tClass == Long.
class) {
return (
T)(Long)sp.getLong(key,
0L); }
else if (tClass == Boolean.
class) {
return (
T)(Boolean)sp.getBoolean(key,
false); }
else { String s = sp.
getString(key,
"");
byte[] bytes = Base64.
decode(s, Base64.
NO_WRAP); ByteArrayInputStream byteArrayInputStream =
new ByteArrayInputStream(bytes); ObjectInputStream objectInputStream;
try { objectInputStream =
new ObjectInputStream(byteArrayInputStream);
try {
return (
T)objectInputStream.readObject(); }
catch (ClassNotFoundException e) { e.printStackTrace(); } }
catch (IOException e) { e.printStackTrace(); }
return null; } }
public List<
T> getList(String key) { SharedPreferences sp =
context.getSharedPreferences(
fileName, Context.
MODE_PRIVATE); String s = sp.
getString(key,
""); Class<
T> tClass = (Class<
T>) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[
0];
byte[] bytes = Base64.
decode(s, Base64.
NO_WRAP); ByteArrayInputStream byteArrayInputStream =
new ByteArrayInputStream(bytes); ObjectInputStream objectInputStream =
null;
try { objectInputStream =
new ObjectInputStream(byteArrayInputStream);
try {
//我不知道能否直接强转 sorry
return (List<
T>) objectInputStream.readObject(); }
catch (ClassNotFoundException e) { e.printStackTrace(); } }
catch (IOException e) { e.printStackTrace(); }
return null; }}
如果用fastJson会很方便,但是这是工具类,为了这个工具类去下载一个jar包?肯定不划算啊,所以必须也要封装一个流转化形式的
使用起来非常方便
new 一下,set一下FileName,条理很清晰,最后放就是put,取就是get