1

I am trying to make a simple app that tracks work time. MainActivity features a TextView representing a timer and four buttons, for starting, pausing, resuming and stopping.

So far I have two classes for modelling the timer, TimerCountdown and TimerCountup (for overtime) that initiate timers using android.os.CountDownTimer;, update the TextView and trigger a system notification to notify the user once they have finished. TimerCountdown updates tView every second and after 8 hours it will send a notification to the user and initiate TimerCountup to track overtime for another 2 hours.

What is the correct way to ensure those timers keep running if the application is in the background? Do I have to wrap the two Timer classes into a Service? If so, how would I do this, since they are holding references to the context, which is apparently not allowed? Unfortunately the documentation for android.os.CountDownTimer; does not mention how it behaves when the applications enters the background. In order to restore progress once the application is destroyed I will probably use a serialized object eventually, I am just not sure how to properly handle background activity.

Also, what would be the best way to ensure the notification (such as a vibration) gets triggered after the timer reaches 0? The app will probably be either in background by then, or destroyed.

KUSH42
  • 552
  • 2
  • 13

1 Answers1

0

If i understand you correctly you want a timer which will work at the background,to do this you should extend service class.There is an answered question which is similar to yours.You can check it out Android count down timer in background

Community
  • 1
  • 1
Alperen Kantarcı
  • 1,038
  • 9
  • 27
  • Thanks, refactoring the two Timer classes into one, extending it into a service and calling startForeground(); did what I was looking for – KUSH42 Feb 13 '17 at 01:56