Android SharedPreferences保存对象以及读取对象信息

本文介绍了一个简单的示例,演示如何使用SharedPreferences在Android应用中保存对象状态。通过自定义类GiftStateInfo和封装的SharedPreferencesUtils,实现了对象状态的序列化与反序列化过程。包括对象状态的获取与设置方法,以及实例化的操作流程。

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

之前用SharedPreferences只是保存了基础数据,最近做项目时需要保存一些对象的状态,自己稍微做了个简单demo,以备后用

1.首先是一段需要保存的礼包状态信息

public class GiftStateInfo implements Serializable {

	private static final long serialVersionUID = 1L;
	public int giftId;
	public int[] temp_state=new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1};
	
	public int getGiftId() {
		return giftId;
	}
	public void setGiftId(int giftId) {
		this.giftId = giftId;
	}
	public int[] getTemp_state() {
		return temp_state;
	}
	public void setTemp_state(int[] temp_state) {
		this.temp_state = temp_state;
	}
	

}

2.自己常用的封装的一个保存类

public class SharedPreferencesUtils {
	public static String fileName = "mySharedPreferences";
	public static SharedPreferences sharedPreferences = null;

	public static SharedPreferences getSharedPreferences(Context context) {
		if (sharedPreferences == null) {
			sharedPreferences = context.getSharedPreferences(fileName,
					Context.MODE_WORLD_READABLE);
		}
		return sharedPreferences;
	}

	/** 设置String */
	public static void setValueByKey(Context context, String key, String value) {
		getSharedPreferences(context).edit().putString(key, value).commit();
	}

	/** 设置Long */
	public static void setLongByKey(Context context, String key, Long value) {
		getSharedPreferences(context).edit().putLong(key, value).commit();
	}

	/** 设置Float */
	public static void setFloatByKey(Context context, String key, Float value) {
		getSharedPreferences(context).edit().putFloat(key, value).commit();
	}

	/** 设置int */
	public static void setIntByKey(Context context, String key, int value) {
		getSharedPreferences(context).edit().putInt(key, value).commit();
	}

	/** 设置boolean */
	public static void setBooleanByKey(Context context, String key,
			Boolean value) {
		getSharedPreferences(context).edit().putBoolean(key, value).commit();
	}

	/** 取出String */
	public static String getValueByKey(Context context, String key) {
		return getSharedPreferences(context).getString(key, "");
	}


	/** 取出Long */
	public static Long getLongByKey(Context context, String key) {
		return getSharedPreferences(context).getLong(key, 0L);
	}

	/** 取出Float */
	public static Float getFloatByKey(Context context, String key) {
		return getSharedPreferences(context).getFloat(key, 0f);
	}

	/** 取出int */
	public static int getIntByKey(Context context, String key) {
		return getSharedPreferences(context).getInt(key, 0);
	}

	/** 取出boolean */
	public static boolean getBooleanByKey(Context context, String key,boolean defaults) {
		return getSharedPreferences(context).getBoolean(key, defaults);
	}

}

3. 主界面简单布局类

public class MainActivity extends Activity implements OnClickListener {

	Button btn01,btn02,btn05;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn01=(Button) findViewById(R.id.btn01);
		btn02=(Button) findViewById(R.id.btn02);
		btn05=(Button) findViewById(R.id.btn05);
		btn01.setOnClickListener(this);
		btn02.setOnClickListener(this);
		btn05.setOnClickListener(this);
	}
	public void saveGiftState(int position) throws Throwable {
		GiftStateInfo mobile = new GiftStateInfo();

		mobile.giftId = position;
		mobile.temp_state[position] =1;
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				byteArrayOutputStream);
		objectOutputStream.writeObject(mobile);
		String mobilesString = new String(Base64.encode(
				byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
		SharedPreferencesUtils.setValueByKey(getApplicationContext(), "giftstate"+mobile.giftId, mobilesString);
		objectOutputStream.close();
	}
	

	public void readGiftState(int position) throws Throwable, Throwable {

		String mobilesString=SharedPreferencesUtils.getValueByKey(getApplicationContext(), "giftstate"+position);
		byte[] mobileBytes = Base64.decode(mobilesString.getBytes(),
				Base64.DEFAULT);
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				mobileBytes);
		ObjectInputStream objectInputStream = new ObjectInputStream(
				byteArrayInputStream);
		GiftStateInfo mobileInfo = (GiftStateInfo) objectInputStream.readObject();
		Toast.makeText(this,
				"游戏礼包id:" + mobileInfo.giftId + "\n 礼包状态:" +Arrays.toString(mobileInfo.temp_state),
				Toast.LENGTH_LONG).show();
		objectInputStream.close();

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn01:
			try {
				saveGiftState(2);
				saveGiftState(5);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case R.id.btn02:
			try {
				readGiftState(2);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case R.id.btn05:
			try {
				readGiftState(5);
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;

		default:
			break;
		}
	}

}


代码地址;

http://download.youkuaiyun.com/detail/buluodebeidou/8067487


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值