MainActivity如下:
package cc.object;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import android.os.Bundle;
import android.util.Base64;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
* Demo描述:
* 利用SharedPreferences存取对象Object
* 注意该Object需要实现Serializable接口
*
* 参考资料:
* 1 http://blog.csdn.net/tangnengwu/article/details/37881087
* 2 http://blog.csdn.net/lfdfhl/article/details/37742775
* 3 http://blog.csdn.net/lfdfhl/article/details/17998469
* 4 http://blog.csdn.net/lfdfhl/article/details/10961459
* Thank you very much
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
saveObjectToSharedPreferences();
getObjectFromSharedPreferences();
}
/**
* 将Object对象保存至SharedPreferences
*/
private void saveObjectToSharedPreferences(){
try {
User user=new User("zxx", "95279527");
SharedPreferences sharedPreferences=getSharedPreferences("user", Context.MODE_PRIVATE);
Editor editor=sharedPreferences.edit();
//第一步:将对象保存至对象输出流ObjectOutputStream
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(user);
//第二步:将ByteArrayOutputStream中的对象转换成字节数组byte
byte[] byteArray=byteArrayOutputStream.toByteArray();
byte[] base64ByteArray=Base64.encode(byteArray, Base64.DEFAULT);
//第三步:将Base64字节数组转换成String且保存至SharedPreferences
String userString=new String(base64ByteArray);
editor.putString("userString", userString);
editor.commit();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 从SharedPreferences获取对象
*/
private void getObjectFromSharedPreferences(){
try {
SharedPreferences sharedPreferences=getSharedPreferences("user", Context.MODE_PRIVATE);
//第一步:从SharedPreferences取出字符串形式的Object
String userString=sharedPreferences.getString("userString", "");
//第二步:将字符串转换成Base64字节数组
byte[] byteArray=userString.getBytes();
byte[] base64ByteArray=Base64.decode(byteArray, Base64.DEFAULT);
//第三步:将字节数组存入ObjectInputStream
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(base64ByteArray);
ObjectInputStream objectInputStream=new ObjectInputStream(byteArrayInputStream);
//第四步:从ObjectInputStream中读取出对象Object
User user=(User) objectInputStream.readObject();
System.out.println("Object from SharedPreferences:"+user);
} catch (Exception e) {
}
}
}
User如下:
package cc.object;
import java.io.Serializable;
public class User implements Serializable {
private String username;
private String password;
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="利用SharedPreferences存取对象"
android:layout_centerInParent="true"/>
</RelativeLayout>