简体   繁体   中英

Android how to implement timer that doesn't stop if app closes

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM