一般使用Sharedprefences存储的都是基本类型的数据,但遇到需要存储整个对象的时候,分开存储是可以的,但是很麻烦,也会记不清楚。
那么可以使用将对象序列化成一个String数据类型存储在SharedPrefences里,需要这个对象数据的时候在反序列化存储在SharedPrefences的String数据,就得到了这个存储的对象。
首先将需要存储的对象实现 Serializable ,表示这个对象是可序列化的。
我这里存储User这个对象
public class User implements Serializable{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第一步,saveobject:
saveObject(serialize(user),getApplicationContext(),USER);
第二步,取对象:
deSerialization(getObject(getApplicationContext(),USER))
那么,User对象就被SharedPrefences存储到一个手机里。
全部代码:
package com.zhangli.saveobject;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
import com.zhangli.fingerprint.R;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@InjectView(R.id.save_button)
Button save_button;
@InjectView(R.id.save_text)
TextView save_text;
@InjectView(R.id.get_button)
Button get_button;
@InjectView(R.id.get_text)
TextView get_text;
private User user;
private static String USER="user";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
ButterKnife.inject(this);
user=new User();
user.setId(999);
user.setName("zhangli");
}
@OnClick(R.id.save_button)
void saveClick(){
try {
saveObject(serialize(user),getApplicationContext(),USER);
save_text.setText("存入对象的id:"+user.getId()+",名字:"+user.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
@OnClick(R.id.get_button)
void getClick(){
try {
User newUser= (User) deSerialization(getObject(getApplicationContext(),USER));
get_text.setText("得到对象的id:"+newUser.getId()+",名字:"+newUser.getName());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 序列化对象
*
* @param person
* @return
* @throws IOException
*/
public static String serialize(Object person) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(person);
String serStr = byteArrayOutputStream.toString("ISO-8859-1");
serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
objectOutputStream.close();
byteArrayOutputStream.close();
return serStr;
}
/**
* 反序列化对象
*
* @param str
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object deSerialization(String str) throws IOException, ClassNotFoundException {
String redStr = java.net.URLDecoder.decode(str, "UTF-8");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
Object person = objectInputStream.readObject();
objectInputStream.close();
byteArrayInputStream.close();
return person;
}
/**
* 存对象
*
* @param strObject 对象
* @param context
* @param name 存储名
*/
public static void saveObject(String strObject, Context context, String name) {
SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString(name, strObject);
edit.commit();
}
/**
* 取对象
*
* @param context
* @param name 存储名
* @return
*/
public static String getObject(Context context, String name) {
SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
return sp.getString(name, "");
}
}