Sharedpreferences Saving and Reading objects
1. First to serialize the object (the following is a sample of the serialized entity class, which makes it inconvenient to post all the code for the entity Class)
public class Loginresult extends Baseresult implements serializable{private static final long Serialversionuid = 1l;privat e String Mobiletoken;
2. Saving and reading the encapsulation of objects
public class Objectsaveutil {private final static string FILENAME = "Login_data_save";p rivate final static string KEY = "L Oginresult "; /** * Desc: Save Object * @param context * @param key * @param obj to save the object, can only save the object that implements SERIALIZABLE * Modified: */PU Blic static void Saveobject (Context context,object obj) {try {//Save object Sharedpreferences.editor sha Redata = context.getsharedpreferences (FILENAME, 0). Edit (); Writes the serialized result to the byte cache first, in fact allocates a memory space Bytearrayoutputstream bos=new Bytearrayoutputstream (); ObjectOutputStream os=new ObjectOutputStream (BOS); Serializes an object to the byte cache Os.writeobject (obj); Convert serialized data to 16 binary save String bytestohexstring = bytestohexstring (Bos.tobytearray ()); Save the 16 binary array sharedata.putstring (KEY, bytestohexstring); Sharedata.commit (); } catch (IOException e) {e.printstacktrace (); LOG.E ("", "Save obj Failed"); }}/** * desc: Convert Array to 16 * @param bArray * @return * Modified: */public static String bytestohexstring (byte[] barray) {if (Barray = = null) { return null; } if (barray.length = = 0) {return ""; } stringbuffer sb = new StringBuffer (barray.length); String stemp; for (int i = 0; i < barray.length; i++) {stemp = integer.tohexstring (0xFF & Barray[i]); if (Stemp.length () < 2) sb.append (0); Sb.append (Stemp.touppercase ()); } return sb.tostring (); }/** * desc: Gets the Saved Object objects * @param context * @param key * @return * Modified: */public static object Reado Bject (Context context) {try {sharedpreferences sharedata = context.getsharedpreferences (FILENAME, 0); if (Sharedata.contains (key)) {string string = sharedata.getstring (Key, ""); if (Textutils.isempty (string)) {return null; }else{//Convert 16 binary data into an array. Prepare to reverse orderColumn byte[] stringtobytes = Stringtobytes (string); Bytearrayinputstream bis=new Bytearrayinputstream (stringtobytes); ObjectInputStream is=new ObjectInputStream (bis); Returns the deserialized object readobject = Is.readobject (); return readobject; }}} catch (Streamcorruptedexception e) {//TODO auto-generated catch block E.printstack Trace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); }//All exceptions return null return null; }/** * desc: Convert 16 binary data to array * <p> Creator: Nie Xuyang, 2014-5-25 a.m. 11:08:33</p> * @param Data * @return * modifie D: */public static byte[] Stringtobytes (String data) {string Hexstring=data.touppercase (). Trim (); if (Hexstring.length ()%2!=0) {return null; } byte[] Retdata=new byte[hexstring.length ()/2]; for (int i=0;i
Very useful, welcome to copy the use of
Android sharedpreferences Saving and Reading objects