android - 电话休眠时停止服务

标签 android android-service java-threads

我创建了一个 (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/

相关文章:

php - 谷歌 API 客户端 :"{ "错误“: "invalid_grant", "error_description": "Incorrect token type."}”

android - 如何在代码中将小部件添加到主屏幕

android - Kotlin Dagger2 找不到符号 ApplicationModule_ProvideApplicationFactory

android - 整个应用程序的自定义字体

android - PhoneAuthProvider 不工作

java - 为什么 Android Studio Profiler 不显示 AsyncTask 线程上的 Activity

android - 应用程序关闭时运行 android 服务

Android 应用程序崩溃(looper = null)

Java wait() 和 notify() 无法正常工作

java - 当我们拥有currentThread()方法时,为什么Thread类具有静态方法?