Search code examples
androidalarmmanagerlooper

Android: Looper vs AlarmManager


Which one is best for continuous/time-wise server updates.

Some developers use AlarmManager followed by PendingIntent and some also use Looper with Handler.

So can anyone please explain me that which is best for continuous network updates and why.


Solution

  • Which one is best for continuous/time-wise server updates?

    depends if your app suppose to run in background / foreground at that time:

    let's say you are scheduling Runnable via handler to 15 minutes from now.
    if the user will "close" all running activities, the system will probably kill your process if it not running any foreground services/activities after a while to reclaim memory. in that case - your Runnable will never be executed.
    if you want to continuously poll for server updates even when the user navigates away from your app - this option is obviously not good for you. if you choose to update server like that - at least do it from a started Service context- that way your Handler reference will stay allocated even when user navigates from one screen to another...

    on the other hand - providing pending intent to AlarmManager insures that no mater if your process is alive or not - the intent provided to the AlarmManager will wake up your app (and the Service / Activity / broadcast) you provided with the pending intent.

    about your question - I think that none of the two are good solution to get server updates:

    the user probably have 10-20 applications (such Facebook/Twitter/Whattsap/Viber/Google+...) installed on his device which getting updates from server. imagine that each one of them wake up in some different time interval, opens internet connections, consuming lot's of precious battery life and band-width. that's crazy! your device will never "sleep", all process will be opened all the time, internet cellular radio transmission also. the battery will rich to 0% very fast!

    the right think to do is to use the GCM API. it requires also server side implementation, but the general idea is that the responsibility to wake up your application when there is a new data passes to the server's side, and by that -the android application don't need to poll for updates. it just get notified (and wake up if the process not alive) when the server notified you. in case you'll wonder - this is how it works - How does push notification technology work on Android?

    I advice you to read about it - http://developer.android.com/google/gcm/index.html