Best Way to Cache Data in Android

前端 未结 3 1997
日久生厌
日久生厌 2021-01-30 05:52

I have an ArrayList of custom, simple Serializable objects I would like to cache to disk and read on re-launch. My data is very small, about 25 object

3条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 06:19

    public class MyClass implements Serializable 
    {
    private static final long serialVersionUID = 1L;
    
    public String title;
    public String startTime;
    public String endTime;
    public String day;
    
    public boolean classEnabled;
    
    
    public MyClass(String title, String startTime, boolean enable) {
        this.title = title;
        this.startTime = startTime;
        this.classEnabled = enable;
    }
    
    public boolean saveObject(MyClass obj) {   
        final File suspend_f=new File(SerializationTest.cacheDir, "test");
    
        FileOutputStream   fos  = null;
        ObjectOutputStream oos  = null;
        boolean            keep = true;
    
        try {
            fos = new FileOutputStream(suspend_f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
        } catch (Exception e) {
            keep = false;
        } finally {
            try {
                if (oos != null)   oos.close();
                if (fos != null)   fos.close();
                if (keep == false) suspend_f.delete();
        } catch (Exception e) { /* do nothing */ }
        }
    
        return keep;
    }
    
    public MyClass getObject(Context c) {
        final File suspend_f=new File(SerializationTest.cacheDir, "test");
    
        MyClass simpleClass= null;
        FileInputStream fis = null;
        ObjectInputStream is = null;
    
        try {
            fis = new FileInputStream(suspend_f);
            is = new ObjectInputStream(fis);
            simpleClass = (MyClass) is.readObject();
        } catch(Exception e) {
            String val= e.getMessage();
        } finally {
            try {
                if (fis != null)   fis.close();
                if (is != null)   is.close();
            } catch (Exception e) { }
        }
    
        return simpleClass;  
    }
    

    and calling from activity

     if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"MyCustomObject");
    else
    cacheDir= getCacheDir();
    if(!cacheDir.exists())
    cacheDir.mkdirs();
    
    MyClass m = new MyClass("umer", "asif", true);
    boolean result = m.saveObject(m);
    
    if(result)
    Toast.makeText(this, "Saved object", Toast.LENGTH_LONG).show();
    
    else
    Toast.makeText(this, "Error saving object", Toast.LENGTH_LONG).show();   
    
     MyClass m = new MyClass();
     MyClass c = m.getObject(this);
    
     if(c!= null)
    
     Toast.makeText(this, "Retrieved object", Toast.LENGTH_LONG).show();
    
      else
    
     Toast.makeText(this, "Error retrieving object", Toast.LENGTH_LONG).show();
    

    dont forget to use write_external_storage permissions in manifest file.

提交回复
热议问题