0

I want app to start a timer at certain point and run for four hour and is displayed in UI. Now problem with using service is that it closes if app is closed. (Same with Handler) And if I use system time than if someone changes system time, 4 hours extends So is there a better way to implement such task?

Akshit
  • 140
  • 3
  • 13
  • Please explain better what are you trying to achieve, because depending on that, there may be different approaches to implement a solution – JML Dec 30 '14 at 17:11

2 Answers2

0

You can make services not close when the activity (visible) is stopped. One way to guarantee it stays alive is to make it a foreground service (will show a notification). But if you don't make it a foreground service it will still last for some time unless your device is running out of battery for example.

In your case, to make it last four hours, perhaps use the foreground service alternative (showing a notification)

Foreground service Vogella foreground service

fersarr
  • 3,399
  • 3
  • 28
  • 35
0

AlarmManager should help see page http://developer.android.com/reference/android/app/AlarmManager.html

Luca Rocchi
  • 6,272
  • 1
  • 23
  • 23