SharedPreferences封装各种数据类型存取

该博客主要介绍了如何在Android中封装和使用SharedPreferences进行数据存储,包括基本类型如String、Int、Boolean等,以及如何存储和读取Bean对象。示例代码展示了存取数据的API方法,方便快捷。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

中间用到了AES加密,代码在AES那篇文章里有介绍AES加密

public class SPTool {
    //BuildConfig.APPLICATION_ID为项目包名
	private static final String SP_NAME = BuildConfig.APPLICATION_ID;
	private static SharedPreferences.Editor editor;
	private static SharedPreferences sp;

	private SPTool() {

	}

	private static void init(Context context) {
		if (editor == null) {
			sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
			editor = sp.edit();
		}
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param defValue
	 */
	public static int getInt(Context context, String key, int defValue) {
		init(context);
		return sp.getInt(key, defValue);
	}

	public static String getString(Context context, String key, String defValue) {
		init(context);
		return sp.getString(key, defValue);
	}

	public static boolean getBoolean(Context context, String key, boolean defValue) {
		init(context);
		return sp.getBoolean(key, defValue);
	}

	public static long getLong(Context context, String key, long defValue) {
		init(context);
		return sp.getLong(key, defValue);
	}

	public static float getFloat(Context context, String key, float defValue) {
		init(context);
		return sp.getFloat(key, defValue);
	}
	
	public static void putInt(Context context, String key, int value) {
		init(context);
		editor.putInt(key, value);
		editor.commit();
	}

	public static void putString(Context context, String key, String value) {
		init(context);
		editor.putString(key, value);
		editor.commit();
	}

	public static void putBoolean(Context context, String key, boolean value) {
		init(context);
		editor.putBoolean(key, value);
		editor.commit();
	}

	public static void putFloat(Context context, String key, float value) {
		init(context);
		editor.putFloat(key, value);
		editor.commit();
	}

	public static void putLong(Context context, String key, long value) {
		init(context);
		editor.putLong(key, value);
		editor.commit();
	}

	public static void clear(Context context) {
		init(context);
		editor.clear();
		editor.commit();
	}

	public static void remove(Context context, String key) {
		init(context);
		editor.remove(key);
		editor.commit();
	}

	/**
	 * 读取已选图片路径
	 */
	public static List<String> getSeletedImages(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		String images = sharedPreferences.getString(key, "");
		List<String> list = new ArrayList<String>();
		if (TextUtils.isEmpty(images)) {
			return list;
		}
		String[] imgArr = images.split(":");
		for (String item : imgArr) {
			if (TextUtils.isEmpty(item)) {
				continue;
			}
			list.add(item);
		}
		return list;
	}

	/**
	 * 读取已选网络图片路径
	 */
	public static List<String> getSeletedNetImages(Context context, String key) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		String images = sharedPreferences.getString(key, "");
		List<String> list = new ArrayList<String>();
		if (TextUtils.isEmpty(images)) {
			return list;
		}
		String[] imgArr = images.split(",");
		for (String item : imgArr) {
			if (TextUtils.isEmpty(item)) {
				continue;
			}
			list.add(item);
		}
		return list;
	}

	/**
	 * 保存已选图片路径
	 */
	public static void saveSelectedImags(Context context, String key, List<String> imgList) {
		init(context);
		if (imgList == null) {
			return;
		}
		StringBuffer result = new StringBuffer("");
		for (String item : imgList) {
			result.append(item).append(":");
		}
		editor.putString(key, result.toString());
		editor.commit();
	}

	/**
	 * 保存bean
	 * @param context
	 * @param t
	 * @param keyName
	 * @param <T>
	 */
	public static <T> void saveBean2Sp(Context context, T t, String keyName) {
		init(context);
		ByteArrayOutputStream bos;
		ObjectOutputStream oos = null;
		try {
			bos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(bos);
			oos.writeObject(t);
			byte[] bytes = bos.toByteArray();
			String ObjStr = Base64.encodeToString(bytes, Base64.DEFAULT);
			editor.putString(keyName, ObjStr);
			editor.commit();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (oos != null) {
				try {
					oos.flush();
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 读取bean
	 * @param context
	 * @param keyNme
	 * @param <T>
	 * @return
	 */
	public static <T extends Object> T getBeanFromSp(Context context, String keyNme) {
		init(context);
		byte[] bytes = Base64.decode(sp.getString(keyNme, ""), Base64.DEFAULT);
		ByteArrayInputStream bis;
		ObjectInputStream ois = null;
		T obj = null;
		try {
			bis = new ByteArrayInputStream(bytes);
			ois = new ObjectInputStream(bis);
			obj = (T) ois.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return obj;
	}
}

二、调用

1、String类型

1).存

 SPTool.putString(LoginActivity.this, Consts.SP_PHONE, edtUname.getText().toString().trim());

1).取

 SPTool.getString(LoginActivity.this, Consts.SP_PHONE);

2、Int类型

1).存

 SPTool.putInt(LoginActivity.this, Consts.SP_LOGIN_TYPE, 1);

1).取

 SPTool.getInt(LoginActivity.this, Consts.SP_LOGIN_TYPE);

3、Boolean类型

1).存

 SPTool.setBoolean(LoginActivity.this, Consts.SP_IS_LOGIN, true);

1).取

 SPTool.getBoolean(LoginActivity.this, Consts.SP_IS_LOGIN);

3、Long类型

1).存

 SPTool.setLong(LoginActivity.this, Consts.SP_UUID, 10000000000L);

1).取

 SPTool.getLong(LoginActivity.this, Consts.SP_UUID);

3、Long类型

1).存

 SPTool.setLong(LoginActivity.this, Consts.SP_UUID, 10000000000L);

1).取

 SPTool.getLong(LoginActivity.this, Consts.SP_UUID);

还有bean的存储的封装等等,代码里都有封装,调用即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值