2013-04-05 55 views
1

我有一个实现了Parcelable的类,并且无法实现Serializable,因为它包含一些我无法修改的基本Android类。保存可分析数据

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

我的问题是保存我的主要活动的实例之间的这种信息。

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

我试图写这个Parcelable到一个文件,但使用marshall()并不总是工作(我越来越活页夹不能编组错误)。

如何安全地保存这些信息?

感谢

+0

考虑这些选项:http://developer.android.com/guide/topics/data/data-storage.html – 2013-04-05 19:32:59

回答

2

我使用一个状态控制类来处理读取/写入到磁盘后重新创建aidlBinder对象:

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; 
     } 

    } 

} 

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

3

使用您的示例中的静态导致内存泄漏,而不是做任何事情的好办法。

我建议仅3例使用静态:

  1. 静态最终字符串或INT - 常量在内部类
  2. (以便它们不包含引用外部类)上的util
  3. 或在某些情况下(如CustomFragment.newInstance)工厂方法

问题是为什么要坚持PendingIntent?它的用例是用于进程间通信。

+0

这实际上是一个很好的问题。我改变了设计,谢谢! – Mugen 2013-04-05 21:15:56

0

粘结剂

大多数开发商不会直接实现这个类,而是采用 的AIDL工具来描述所需的接口,有它产生 适当的活页夹的子类。

from the official documentation

你需要的Binder对象存储与对象的其余部分?也许你可以保存你的对象没有Binder实例,并且恢复对象