2

我调用了Service几次,服务执行它的工作,然后停止自己。

我需要的是在同一服务的连续调用之间存储一个卑鄙的人,

以便:

  • 每次调用服务时,它都会检索对象使用

  • 并在停止使用它之前再次保存它,以便在下一次调用中使用它。

所讨论的对象是RemoteViews对象。

4

1 回答 1

1

使用可以使用单例模式:

public class SingletonClass{
   private YourObject objectToStoreBetweenSession;
   private SigletonClass instance;
   private SingletonClass(){
       objectToStoreBetweenSession = new YourObject();
   }

   private static SingletonClass getInstance(){
      if(instance==null)
         instance = new SingletonClass();
      return instance;
   }
   public void setObject(YourObject obj){
      objectToStoreBetweenSession = obj;
   }
   public YourObject getObject(){
      return objectToStoreBetweenSession;
   }
}

进入你的Service

YourObject objectToStoreBetweenSession = SingletonClass.getInstance().getObject();
if(objectToStoreBetweenSession.value==0){
   //First time that Service is called.
}else{
   //Do whatever you want
   SingoletonClass.getInstance().setObject(new YourObject("value"));
}
于 2013-03-31T17:23:03.083 回答