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?
Asked
Active
Viewed 619 times
2 Answers
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
-
-
yes, it refer to System.currentTimeMillis() see code example here http://stackoverflow.com/questions/4459058/alarm-manager-example – Luca Rocchi Dec 31 '14 at 10:23
-
But that will be problem there right? If someone changes system time say 1 hour back? – Akshit Dec 31 '14 at 10:35