4

I'm working on an app which needs to send data to a webserver after every x minutes (where x is a value that can be changed through webserver responses).

Right now, I'm using a background thread that sleeps for x minutes and then notifies the main thread to start sending data.

However, I'm experiencing issues with this method due to CPU-sleeping, which seems to affect the thread sleeping time.

I've read about wakelocks and think I could implement a partial wakelock...however, one of the core features of this app is to save battery, so overall wakelocks just aren't feasible.

So I was wondering if there are any alternatives to wakelocks that can make sure that a certain piece of logic always runs in the background after every x minutes, ideally only waking up the CPU when it is needed?

Thanks in advance.

Micha
  • 5,117
  • 8
  • 34
  • 47
SJunejo
  • 71
  • 1
  • 8
  • Use the AlarmManager to set hard intervals. It will wake up the device if the device is sleeping. While your app does its thing grab a wakelock so that the device doesn't fall asleep and release it after you are done – Srikant Sahay Jul 30 '13 at 06:56

1 Answers1

2

Use SheduledThreadPoolExecutor for this. It can handle single tasks, periodic tasks etc...

Raiv
  • 5,731
  • 1
  • 33
  • 51