Android service stops

前端 未结 1 1761
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 19:14

I\'ve come to the point where I\'m a few steps away from going bald...

I\'m trying to run a service that will check if the servers at my work are running. It\'s supp

相关标签:
1条回答
  • 2020-12-31 19:31

    I've been through TimerTask, ScheduledExecutorService and finally Handler. They all work "fine" for a few hours, except for some inaccuracy, 1-5 mins off, and then suddenly the "timer" just stops firing.

    Of course. None of those are designed for your purpose, as neither keep the device awake, and neither play nicely with the Android framework. Having a service in RAM 24x7 just to mark the passage of time is wasteful, and Android gets more aggressive with each passing release at shutting down everlasting services like this one.

    Use AlarmManager to set up your schedule and an IntentService to do the actual work. The IntentService automatically gives you your background thread for your network I/O, and it automatically shuts down the service when the work is complete.

    If you intend to have this occur even if the device falls asleep, use a _WAKEUP alarm with AlarmManager and my WakefulIntentService (or something along those lines).

    0 讨论(0)
提交回复
热议问题