Search code examples
androidtimerandroid-servicewakelockbackground-service

Android Timer which works on both background and foreground


I'm now working on an app which will track students' studying time. The student can press start button and start studying. And stop when he finish studying.

When screen is on or phone is plugged in,the text view and progress bar is updating every second .(I means it works fine) But when the screen is off,updating the textview and progress is stopped after 10 seconds of sleeping the phone. I think it may be the activity is pausing. Is that right?

I tested by using:

  1. partial wakelock
  2. service

Both of these ways make the battery drains. Is there any idea for this case? Thanks.


Solution

  • Finally I've got an answer. I read the time when at onPause(). And stop updating view.

    At the time of working onResume(). I again read the time. Then I update the view by adding (onResumeTime-onPauseTime) then resume updating.

    So,now I don't need to use service.