0

I think this is pretty much the standard case already described in other SO question but I still need a clarification on this matter:

So I have an Android app with an Actvity and a Service. The Activity is not of interest but the Service. The Service has to send some message to a remote server every minute. From what I understand, I need to use WakeLocks to keep the CPU running while allowing the screen to go off (so that I can fix the problem where the service stops when the screen is powered off). So far so good.

My question is: can I acquire the lock, send the message to the server, release the lock AND acquire it again after one minute so that during this one minute pause the CPU is sleeping, too. With the ultimate goal to save the battery. I fear the answer is "no" because once you let the CPU to sleep, you cannot wake it up unless from a lower level (OS and not app).

Best regards

Anton Sarov
  • 3,712
  • 3
  • 31
  • 48

2 Answers2

1

The response is simple: no. What you can do in this case is set a PendingIntent and use the Android Alarm manager to be woken up every minute.

greywolf82
  • 21,813
  • 18
  • 54
  • 108
1

The alarm manager is the way to go - but you also need to delegate from the alarm receiver to a WakefulIntentService to do the work (as the receiver will ANR after 5 seconds). See PowerManager.PARTIAL_WAKE_LOCK android for links.

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361