保存Parcelable数据 [英] Saving Parcelable data

查看:492
本文介绍了保存Parcelable数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现Parcelable一类,因为它包含了一些基本的Andr​​oid类,我不能修改无法实现Serializable。

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

在一些该类的对象是例如位置和的PendingIntent(这些都是方便Parcelable)。

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.

我想这Parcelable写入文件,但使用马歇尔()并不总是工作(我收到活页夹不能被整理错误)。

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?

感谢

推荐答案

我用一个状态控制类来处理的读/写盘:

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.

这篇关于保存Parcelable数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆