我已經在我的應用程序中使用點火來緩存我的複合對象,比方說mStudentObject。我已經成功緩存了我的數據,問題是,當我從最近運行的應用程序按鈕(從當前正在運行的任務按鈕)中殺死應用程序後檢索到對象時,我還沒有發出任何針對鍵的數據(自動緩存清除)。啓動應用程序(與最近的任務殺出的應用程序)正確檢索的對象。 我不知道代碼有什麼問題。我想永久緩存我的對象2天。當我啓動我的應用程序時,應用程序應該從緩存對象獲取數據,或者從當前正在運行的任務中刪除應用程序。任何想法,請share.Here是我的完整代碼:Android持久緩存:使用ModelCache從點燃
public class MainActivity extends Activity {
Button[] buttons = null;
// ObjectLRUCache objectLRUCache = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttons = new Button[2];
buttons[0] = (Button) findViewById(R.id.button1);// to save data
buttons[1] = (Button) findViewById(R.id.button2); // to get data
// final Student s = new Student("imran", 23, 16);
buttons[0].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (IgnetionHelper.getInstance()!= null) {
Log.d("test", "key contains, updating");
Student s = new Student("imran", 23, 16);
IgnetionHelper.getInstance().putData(s);
} else{
Log.d(""test),"instance is null..");
}
});
buttons[1].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
IgnetionHelper ddd = IgnetionHelper.getInstance();
if (IgnetionHelper.getInstance().getData()!= null) {
Student s = (Student) IgnetionHelper.getInstance().getData();
Log.d("test", "key contains, age is: " + s.age);
} else {
Log.d("test", "data is null...");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
和我的Person類是:
public abstract class Person extends CachedModel implements Serializable{
public String name = "";
public int age = 0;
public Person(){};
public Person (String name,int age) {
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student類是:
public class Student extends Person{
public String name = "";
public int age = 0;
public int rollNo = 0;
public Student(){
}
public Student(String name, int age, int rollno) {
this.rollNo = rollno;
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
@Override
public boolean reloadFromCachedModel(ModelCache modelCache,
CachedModel cachedModel) {
Student student = (Student) cachedModel;
name = student.name;
age = student.age;
rollNo = student.rollNo;
return false;
}
@Override
public String createKey(String id) {
// TODO Auto-generated method stub
return id;
}
}
最後,點火幫手類如下:
public class IgnetionHelper {
private static final String KEY_FOR_MYOBJECT = "MY_TEST_KEY";
private static ModelCache cache;
private final static int initialCapacity = 1000;
private final static int maxConcurrentThreads = 3;
private final static long expirationInMinutes = 60 * 24 * 2;
private static IgnetionHelper mIgnetionHelper = null;
public static IgnetionHelper getInstance() {
if (cache == null)
cache = new ModelCache(initialCapacity, expirationInMinutes,
maxConcurrentThreads);
if (mIgnetionHelper == null)
mIgnetionHelper = new IgnetionHelper();
return mIgnetionHelper;
}
public boolean putData(CachedModel model) {
model.setId(KEY_FOR_MYOBJECT);
if (model.save(cache)) {
Log.d("IgnetionHelper", "saved.....");
return true;
} else {
Log.d("IgnetionHelper", "saved.....");
return false;
}
// CachedModel model = Feed.find(cache, key, Feed.class);
// if (model != null) {
// Log.d("test", "key contains, updating");
// Feed s = (Feed) model;
// return s.save(cache);
// }
}
public CachedModel getData() {
return Student.find(cache, KEY_FOR_MYOBJECT, Student.class);
}
}