我创建了一个 (AutoTestService) 服务来以单周期方式执行某些任务。完成循环后,为下一个循环设置警报以执行任务(警报再次调用AutoTestService)。在每个任务中,我暂停线程 10/20(使用 Thread.sleep)秒并转到下一个任务。服务 Iam 使用 AlarmManager 开始 WakefulBroadcastReceiver。
当手机屏幕打开并且连接到 USB 时,它工作正常。但是,如果没有 USB 的手机 sleep ,我的服务将停止执行。我怀疑手机处于 sleep 状态时 thread.sleep 不会恢复。应用程序运行在 android 5.1 & 5.1.1
任何人都可以提出建议,不胜感激。我为此苦苦挣扎了很长时间。
最佳答案
使用IntentService .
The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. Also, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask
关于android - 电话休眠时停止服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35222140/