1

I am developing an app for android that measures some network parameters every hour. I already have the code to do the measurements, so I only need some help on how to make it do it periodically. What should I use? Timers, Alarms or something else? Do you have any easy example on how it works?

Thanks for your help!

modellero
  • 213
  • 2
  • 6
  • 14

4 Answers4

2

The only good answer is to use AlarmManager. Having a service that sits for an hour waiting for the hour to pass is ridiculous.

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

Using a service (so that you're not tied to an activity) you can use Handler.postAtTime to schedule a task in the future

Handler.postAtTime

Rich
  • 36,270
  • 31
  • 115
  • 154
0

Both should work. How about a service that runs in the background?

DKIT
  • 3,471
  • 2
  • 20
  • 24
  • That's actually what I want to do. I know this is kind of basic, but I'm still just learning... Do you know any tutorial/example to use service? – modellero Feb 10 '12 at 17:25
0

A service may or may not be shut down by the OS. A scheduled task may or may not start because the device might be in "deep sleep" mode. Yo should double check this, use a service and also schedule a task, if the task opens, verify with a lock or any device you want and work accordingly. That way you are almost certain that the readings will be done.

AlfredoVR
  • 4,069
  • 3
  • 25
  • 33