Service is not waking up device as it should

孤者浪人 提交于 2019-12-06 06:40:14

Using AlarmManager.ELAPSED_REALTIME_WAKEUP or AlarmManager.RTC_WAKEUP will wake the device up when the alarm fires.

The device stop requesting the server at some point while it is slept and at some look-like random time it restore it's work for a short. Why this happens, and how to fix this issue?

This might happen because your PendingIntent is calling a Service. This way the device can go back to sleep before onStartCommand() gets executed. You should use a BroadcastReceiver instead (since a WakeLock is "guaranteed" during onReceive()), aquire a WakeLock in onReceive(), start your Service from there and release your WakeLock from the Service, when appropriate.

Although, repeating a request every second for an extended period of time (and even when the device is in sleep) seems like a terrible design. It will drain the battery pretty fast.

You should rethink your implementation, maybe use some sort of a callback mechanism instead, or at least increase the intervals between the requests (significantly).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!