Android SharedPreferences保存对象以及读取对象信息
2014-10-22 11:32
281 查看
之前用SharedPreferences只是保存了基础数据,最近做项目时需要保存一些对象的状态,自己稍微做了个简单demo,以备后用
1.首先是一段需要保存的礼包状态信息
2.自己常用的封装的一个保存类
3. 主界面简单布局类
代码地址;
http://download.csdn.net/detail/buluodebeidou/8067487
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.csdn.net/detail/buluodebeidou/8067487
相关文章推荐
- android本地、sd卡保存对象或集合,以及读取该对象
- android本地、sd卡保存对象或集合,以及读取该对象
- C#中如何将信息保存到txt文件中去,以及如何读取
- android 将图片保存在 array.xml 成string-array 以及读取
- Android SharedPreferences保存和读取对象
- 关于android 将对象写入文件以及从文件读取对象
- android解压zip到sdcard,并读取解压缩后文件中的xml特定信息,并保存到数据库
- android保存读取对象 SharedPreferences方式
- [Android实例] 使用SQLite存储以及读取Drawable对象【转】
- Android 选取图片、手机拍照以及保存图片到本地、读取图片
- android中文件的保存与读取以及文件的四种操作模式
- Android SharedPreferences(信息的保存和读取)
- android数据存储读取3:对象保存(对比IOS)
- dos保存adb logcat读取的Android信息
- Android实现勾选保存用户名登陆信息、进入自动读取用户登陆信息
- python pickle模块使用-将内存数据保存到文本中以及从文本中直接读取信息到内存中
- Android调用原声分享相关的知识以及保存图片读取图片
- Android SharedPreferences保存和读取对象
- Android 模拟登陆 保存密码(信息)到手机中 文件信息读取
- Android将程序崩溃信息保存本地文件以及上传到服务器