您的位置:首页 > 移动开发 > Android开发

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

2014-10-22 11:32 281 查看
之前用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.csdn.net/detail/buluodebeidou/8067487
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: