4

I have an app that needs to send a periodic heart beat to a server, but when the phone goes into standby mode the background heartbeat thread dies. Is there anyway to wake the phone from standby, send the heartbeat and then go back to sleep programmatically? I want to avoid using PARTIAL_WAKE_LOCK if possible.

Thanks

MalcomTucker
  • 7,407
  • 14
  • 72
  • 93

1 Answers1

5

Is there anyway to wake the phone from standby, send the heartbeat and then go back to sleep programmatically?

Use AlarmManager with a _WAKEUP-style alarm. Here is a sample project illustrating its use (along with a WakefulIntentService you will want, to make sure the device does not fall back asleep during your network I/O).

I want to avoid using PARTIAL_WAKE_LOCK if possible.

You cannot do network I/O without a WakeLock, because the device will fall back asleep during the I/O. Using AlarmManager, you can arrange to only hold a WakeLock during the actual heartbeat work, not 100% of the time.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491