android - Chronometer keep in background time spent -


i developing feature in app when click on button on activity launch service start,pause , resume chronometer. have problem how start , stop in background service.

i created activity

public class startworkactivity extends activitygeneraltoolbar {      protected final void oncreate(bundle savedinstancestate)     {         super.oncreate(savedinstancestate, r.layout.activity_start_work);         chronometer chronometer = (chronometer) findviewbyid(r.id.chronometer);        }      @override     protected void onstop() {         super.onstop();     }      public void startwork(view v){         intent msgintent = new intent(startworkactivity.this, worktimerservice.class);           msgintent.setaction("start_timer");         getbasecontext().startservice(msgintent);     }      public void pausework(view v){         intent msgintent = new intent(startworkactivity.this, worktimerservice.class);         msgintent.setaction("pause_timer");      }      public void resumework(view v){        //call service         intent msgintent = new intent(startworkactivity.this, worktimerservice.class);         msgintent.setaction("resume_timer");       }       @override     protected void onpause() {         super.onpause();     }      @override     protected void onresume() {          super.onresume();     } } 

and worktimerservice

public class worktimerservice extends intentservice {      long timewhenstopped = 0;     chronometer chronometer;      public worktimerservice() {         super("systemservice");      }      @nullable     @override     public ibinder onbind(intent intent) {         return null;     }      @override     protected void onhandleintent(intent intent) {         if(intent.getaction() == "start_timer"){             startwork();         }         if(intent.getaction() == "pause_timer"){             pausework();          }if(intent.getaction() == "resume_timer"){             resumework();         }     }      @override     public int onstartcommand(intent intent, int flags, int startid) {         super.onstartcommand(intent, flags,startid);          return start_sticky;     }     public void pausework(){          timewhenstopped = chronometer.getbase() -  systemclock.elapsedrealtime();         chronometer.stop();     }      public void resumework(){          chronometer.setbase(systemclock.elapsedrealtime() + timewhenstopped);         timewhenstopped = 0;         chronometer.start();      }      public void startwork(){          chronometer.start();     } } 

but problem chronometer null in service, because read not possible, in service, interact ui. , so, how can send, or work chronometer in background?

chronometer ui widget (actually textview) in android. so, can't use non-ui purposes. try use timer or countdowntimer instead.

see example usage of timer inside service: https://stackoverflow.com/a/3819721/5250273


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -