保存Parcelable数据(Saving Parcelable data)

编程入门 行业动态 更新时间:2024-10-17 14:28:20
保存Parcelable数据(Saving Parcelable data)

我有一个实现Parcelable的类,并且无法实现Serializable,因为它包含一些我无法修改的基本Android类。

这个类中的一些对象是例如Location和PendingIntent(它们都是方便的Parcelable)。

我的问题是在我的主Activity的实例之间保存此信息。

目前,我正在持有这个类的静态引用,它运行良好。 但我认为当我重新安装应用程序时,可能会在更新时出现,我将无法相信这个静态成员不会被重新初始化。

我试图将这个Parcelable写入一个文件,但是使用marshall()并不总是有效(我得到Binder无法编组错误)。

我怎样才能安全地保存这些信息?

谢谢

I have a class which implements Parcelable, and could not implement Serializable because it contains some basic Android classes that I can not modify.

Some of the objects in this class are for example Location and PendingIntent (which are all conveniently Parcelable).

My problem is saving this information between the instances of my main Activity.

Currently, I'm holding a static reference to this class, which works well. But I assume that when I re-install the app, and probably when updates will come around, I won't be able to trust that this static member won't be re-initialized.

I tried to write this Parcelable to a file, but using marshall() is not always working (I'm getting Binder can't be marshalled error).

How can I safely save this information?

Thanks

最满意答案

我使用StateControl类来处理对光盘的读/写:

public class StateControl { Context mContext; Thread worker; WriteObjectToFile writer; // StateControl Constructor public StateControl(Context context) { mContext = context; // Construct a writer to hold and save the data writer = new WriteObjectToFile(); // Construct a worker thread to handle the writer worker = new Thread(writer); }// end of StateControl constructor // Method to save the global data public void saveObjectData(Object object, String key) { if (object == null){ // I had a different action here } else { // Write the data to disc writer.setParams(new WriteParams(object, key)); worker.run(); } }// end of saveGlobalData method // Method to read the Global Data public Object readObjectData(String key){ Object returnData = (Object) readObjectFromFile(key); if (returnData == null){ // I had a different action here } else { return returnData; } }// end of readGlobalData method // Method to erase the Global data public void clearObjectData(String key){ writer.setParams(new WriteParams(null, key)); worker.run(); }// end of clearGlobalData method private class WriteObjectToFile implements Runnable { WriteParams params; public void setParams(WriteParams params) { this.params = params; } public void run() { writeObjectToFile(params.getObject(), params.getFilename()); } private boolean writeObjectToFile(Object object, String filename) { boolean success = true; ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = mContext.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { success = false; e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nothing } }// end of if }// End of try/catch/finally block return success; } }// end of writeObjectToFile method private Object readObjectFromFile(String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = mContext.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; } private static class WriteParams { Object object; String filename; public WriteParams(Object object, String filename) { super(); this.object = object; this.filename = filename; } public Object getObject() { return object; } public String getFilename() { return filename; } } }

然后调用公共方法开始写/读。 对于这个版本,我也在一个单独的线程中进行,但如果需要,你可以修改它。

I use a StateControl class to handle reading/writing to disc:

public class StateControl { Context mContext; Thread worker; WriteObjectToFile writer; // StateControl Constructor public StateControl(Context context) { mContext = context; // Construct a writer to hold and save the data writer = new WriteObjectToFile(); // Construct a worker thread to handle the writer worker = new Thread(writer); }// end of StateControl constructor // Method to save the global data public void saveObjectData(Object object, String key) { if (object == null){ // I had a different action here } else { // Write the data to disc writer.setParams(new WriteParams(object, key)); worker.run(); } }// end of saveGlobalData method // Method to read the Global Data public Object readObjectData(String key){ Object returnData = (Object) readObjectFromFile(key); if (returnData == null){ // I had a different action here } else { return returnData; } }// end of readGlobalData method // Method to erase the Global data public void clearObjectData(String key){ writer.setParams(new WriteParams(null, key)); worker.run(); }// end of clearGlobalData method private class WriteObjectToFile implements Runnable { WriteParams params; public void setParams(WriteParams params) { this.params = params; } public void run() { writeObjectToFile(params.getObject(), params.getFilename()); } private boolean writeObjectToFile(Object object, String filename) { boolean success = true; ObjectOutputStream objectOut = null; try { FileOutputStream fileOut = mContext.openFileOutput(filename, Activity.MODE_PRIVATE); objectOut = new ObjectOutputStream(fileOut); objectOut.writeObject(object); fileOut.getFD().sync(); } catch (IOException e) { success = false; e.printStackTrace(); } finally { if (objectOut != null) { try { objectOut.close(); } catch (IOException e) { // do nothing } }// end of if }// End of try/catch/finally block return success; } }// end of writeObjectToFile method private Object readObjectFromFile(String filename) { ObjectInputStream objectIn = null; Object object = null; try { FileInputStream fileIn = mContext.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; } private static class WriteParams { Object object; String filename; public WriteParams(Object object, String filename) { super(); this.object = object; this.filename = filename; } public Object getObject() { return object; } public String getFilename() { return filename; } } }

Then invoke the public methods to kick off the writing/reading. For this version, I also having it taking place in a separate thread, but you could modify that if you needed to.

更多推荐

本文发布于:2023-07-15 05:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110753.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Parcelable   data   Saving

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!