2

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.

Marcel50506
  • 1,149
  • 1
  • 12
  • 30
Ranjit
  • 4,797
  • 3
  • 28
  • 62

1 Answers1

1

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

Community
  • 1
  • 1
Tal Kanel
  • 9,855
  • 10
  • 55
  • 93
  • +1 for a good advice. Nice explanation and also very useful for me and many developers. But can you help me to understand why you always prefer GCM( Google Cloud Messaging) . And how this is helpful for time-wise sending/receiving data to server (for ex: gps tracking apps always have to do such kind of things). Is this the perfect decision to use a tough and new thing for this work . Or we will continue with the previous ones(AlalamManager/Looper). – Ranjit Jul 31 '13 at 07:44
  • Ok ..Its good when we require the updated server data. i.e when server will found some new data it will immediately push it to the client. Fine. but what about the client(vice versa) ? how it will send continuous updated datas (ex: latitude, longitude) to the server.. – Ranjit Jul 31 '13 at 08:08
  • I explained you exactly why it's much better. you did not mentioned in your question anything about the opposite direction (sending to the server your current location). anyway - if you want to send the server information when location changed - you have to do it carefully because the reasons I already mentioned. and your question "using Handlers vs AlarmManager" is not relevant - because if you are using google best practices - you have to set explicitly location listener callback or provide pending intent. there's nothing to do here with handler – Tal Kanel Jul 31 '13 at 08:18
  • I aware about all these things and about my question also. But thanks a lot for clearing my doubts with a nice advice. I will must use GCM in my continuing apps. thank you – Ranjit Jul 31 '13 at 08:28