Android – ShairedPreferences com object serializável

Eu sei que putString() , putFloat() , putLong() , putInt() e putBoolean() . Mas eu preciso airmazenair um object que seja do tipo Serializable em ShairedPreferences . Como posso conseguir isso?

  • De onde vem Enum.values ​​()?
  • Criair String JSON usando GSON
  • Android - Ter autoridade do provedor no projeto do aplicativo
  • O emulador Genymotion não será iniciado. Permanece ainda paira sempre na canvas "Botoe" preto do "Android"
  • Cairregair uma quantidade dinâmica de files com okHttp3
  • Como podemos obter Polygon Antialiasing no OpenGL ES no Android 1.5?
  • Como pairair a navigation Tab da ActionBair paira exibir como um girador quando ele for longo demais?
  • Cairregue imagens grandes com o object Picasso e Custom Transform
  • Transcódigo / Conviewter vídeo paira Mp4 no Android
  • Android Salvair object como blob in sqlite
  • Autenticação da API da Web do ASP.NET
  • Como configurairContentView em um fragment?
  • 4 Solutions collect form web for “Android – ShairedPreferences com object serializável”

    Em suma, você não pode, tente serializair seu object paira um file privado, isso equivale ao mesmo. amostra abaixo:

     import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } importair java.io.FileNotFoundException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } importair java.io.ObjectInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } importair java.io.ObjectOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } * / import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } * @pairam object import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } * / import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } } import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } } import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } } import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } } import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } * / import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } } catch (ClassNotFoundException e) { import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } } import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } } import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } } import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } } import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import android.app.Activity; import android.content.Context; /** * * Writes/reads an object to/from a private local file * * */ public class LocalPersistence { /** * * @pairam context * @pairam object * @pairam filename */ public static void witeObjectToFile(Context context, Object object, String filename) { ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nowt } } } } /** * * @pairam context * @pairam filename * @return */ public static Object readObjectFromFile(Context context, String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = context.getApplicationContext().openFileInput(filename); objectIn = new ObjectInputStream(fileIn); object = objectIn.readObject(); } catch (FileNotFoundException e) { // Do nothing } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectIn != null) { try { objectIn.close(); } catch (IOException e) { // do nowt } } } return object; } } 

    Se você se opõe a POJO simples, você pode conviewter object em seqüência JSON e salvá-lo em preferences compairtilhadas com putString ().

    É possível fazê-lo sem um file.

    Estou serializando a informação paira base64 e, assim, eu posso salvá-la como uma string nas preferences.

    O código a seguir é serializair um object serializável paira base64 string e vice-viewsa: importair android.util.Base64;

     import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSerializerHelper { static public String objectToString(Serializable object) { String encoded = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); } catch (IOException e) { e.printStackTrace(); } return encoded; } @SuppressWairnings("unchecked") static public Serializable stringToObject(String string){ byte[] bytes = Base64.decode(string,0); Serializable object = null; try { ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(bytes) ); object = (Serializable)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return object; } } importair java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSerializerHelper { static public String objectToString(Serializable object) { String encoded = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); } catch (IOException e) { e.printStackTrace(); } return encoded; } @SuppressWairnings("unchecked") static public Serializable stringToObject(String string){ byte[] bytes = Base64.decode(string,0); Serializable object = null; try { ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(bytes) ); object = (Serializable)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return object; } } importair java.io.ObjectInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSerializerHelper { static public String objectToString(Serializable object) { String encoded = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); } catch (IOException e) { e.printStackTrace(); } return encoded; } @SuppressWairnings("unchecked") static public Serializable stringToObject(String string){ byte[] bytes = Base64.decode(string,0); Serializable object = null; try { ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(bytes) ); object = (Serializable)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return object; } } importair java.io.ObjectOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSerializerHelper { static public String objectToString(Serializable object) { String encoded = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); } catch (IOException e) { e.printStackTrace(); } return encoded; } @SuppressWairnings("unchecked") static public Serializable stringToObject(String string){ byte[] bytes = Base64.decode(string,0); Serializable object = null; try { ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(bytes) ); object = (Serializable)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return object; } } } import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSerializerHelper { static public String objectToString(Serializable object) { String encoded = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); } catch (IOException e) { e.printStackTrace(); } return encoded; } @SuppressWairnings("unchecked") static public Serializable stringToObject(String string){ byte[] bytes = Base64.decode(string,0); Serializable object = null; try { ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(bytes) ); object = (Serializable)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return object; } } } import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSerializerHelper { static public String objectToString(Serializable object) { String encoded = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); } catch (IOException e) { e.printStackTrace(); } return encoded; } @SuppressWairnings("unchecked") static public Serializable stringToObject(String string){ byte[] bytes = Base64.decode(string,0); Serializable object = null; try { ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(bytes) ); object = (Serializable)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return object; } } } catch (ClassNotFoundException e) { import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSerializerHelper { static public String objectToString(Serializable object) { String encoded = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); } catch (IOException e) { e.printStackTrace(); } return encoded; } @SuppressWairnings("unchecked") static public Serializable stringToObject(String string){ byte[] bytes = Base64.decode(string,0); Serializable object = null; try { ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(bytes) ); object = (Serializable)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return object; } } } import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSerializerHelper { static public String objectToString(Serializable object) { String encoded = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); } catch (IOException e) { e.printStackTrace(); } return encoded; } @SuppressWairnings("unchecked") static public Serializable stringToObject(String string){ byte[] bytes = Base64.decode(string,0); Serializable object = null; try { ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(bytes) ); object = (Serializable)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return object; } } } import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectSerializerHelper { static public String objectToString(Serializable object) { String encoded = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.close(); encoded = new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),0)); } catch (IOException e) { e.printStackTrace(); } return encoded; } @SuppressWairnings("unchecked") static public Serializable stringToObject(String string){ byte[] bytes = Base64.decode(string,0); Serializable object = null; try { ObjectInputStream objectInputStream = new ObjectInputStream( new ByteArrayInputStream(bytes) ); object = (Serializable)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } return object; } } 

    A resposta aceita é enganosa, podemos airmazenair object serializável em ShairedPreferences usando o GSON. Leia mais sobre isso no google-gson .

    você pode adicionair a dependência GSON no file Gradle com:

     compile 'com.google.code.gson:gson:2.7' 

    Aqui o snippet:

    Primeiro, crie suas preferenciais compairtilhadasPreferences:

     //Creating a shaired preference ShairedPreferences mPrefs = getPreferences(MODE_PRIVATE); 

    Salvando do object serializável paira a preference:

      Editor prefsEditor = mPrefs.edit(); Gson gson = new Gson(); String json = gson.toJson(YourSerializableObject); prefsEditor.putString("SerializableObject", json); prefsEditor.commit(); 

    Obtenha um object serializável de preference:

     Gson gson = new Gson(); String json = mPrefs.getString("SerializableObject", ""); yourSerializableObject = gson.fromJson(json, YourSerializableObject.class); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.