0

My question is rather simple but might come with a complex answer.

I'm making an App that checks on an online mysql db (via a php script on the website) for new updates. Sometimes this updates will tell the App it has to download form a FTP server.

The App should start on boot and check for updates every 15 minutes.

I've read in the web I should either use a service or a AlarmManager but I don't know which one is better.

Also, I've read a lot of pages that say that AlarmManager will "Wake Up" the device but I've failed to understand what this really means and why it's different in a service. Does this means that if the Phone is turned off it will turn it on or that it will turn on the screen?

I only need the phone to do the task in the background when it's on, I don't need it to turn the screen on or power up the device.

Luciano Stupenengo
  • 183
  • 1
  • 1
  • 12

1 Answers1

1

I've read in the web I should either use a service or a AlarmManager but I don't know which one is better.

It's not an "or". It's an "and". You will need to use AlarmManager to trigger the work to be done by a Service.

I've read a lot of pages that say that AlarmManager will "Wake Up" the device but I've failed to understand what this really means

An AlarmManager _WAKEUP event type (e.g., ELAPSED_REALTIME_WAKEUP) will wake up the device out of sleep mode. That, in conjunction with something like WakefulBroadcastReceiver and an IntentService, can arrange for you to do your work periodically even though the device would ordinarily be asleep (screen and CPU in a sleep state).

I only need the phone to do the task in the background when it's on, I don't need it to turn the screen on or power up the device.

Then you can use AlarmManager with a non-_WAKEUP alarm type (e.g., ELAPSED_REALTIME). I would still recommend using WakefulBroadcastReceiver and an IntentService, to make sure that the device does not fall asleep in the middle of what you are doing, as that may cause problems for your work.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So far I was trying to make an event every 15 minutes with AlarmManager that fired a AsynkTask with opened the xxx.com/xxx.php and then if needed open a FTP conection in the same AsynkTask. I'm starting to understand this is a bad idea, it could be interrupted? – Luciano Stupenengo Dec 16 '14 at 22:34
  • 1
    @LucianoStupenengo: "with AlarmManager that fired a AsynkTask" -- that is not possible. `AlarmManager` invokes a `PendingIntent`, which will either start an activity, start a service, or send a broadcast. It cannot directly run an `AsyncTask`. "it could be interrupted?" -- the device can fall asleep or the process could be terminated. Using a `Service` (such as an `IntentService`) would help avoid the process termination scenario. Using `WakefulBroadcastReceiver` will help with the device falling asleep scenario. – CommonsWare Dec 16 '14 at 22:54
  • Ok now, I think you've set me in the right path. I have the AlarmManager and I'm reading how to use WakefullBroadcasReceiver, then once I undestand this I'll use an IntentService to make open my PHP and FTP. – Luciano Stupenengo Dec 16 '14 at 23:03
  • 1
    @LucianoStupenengo: FWIW, here is a sample project demonstrating the use of `AlarmManager, `WakefulBroadcastReceiver`, and `IntentService`: https://github.com/commonsguy/cw-omnibus/tree/master/AlarmManager/WakeCast – CommonsWare Dec 16 '14 at 23:04