0

I would like to develop an application - a service - that updates the user location to server, say, every 1 hour.

I understand from the Android docs that I have to use "Android Services" and I have to do the work on the onStartCommand()

But since I need to keep sending these location updates every 1 hour, then I need to run infinite loop inside onStartCommand() ... right?

Now, my questions are:

  • What will happen if system kill the service, I know my service will get started, but would it call onStartCommand() also?

  • Is there a better way to implement location update, for example, is it possible that the system call the onStartCommand() method periodically every 1 hour?

Hope4You
  • 1,927
  • 4
  • 21
  • 45
user836026
  • 10,608
  • 15
  • 73
  • 129
  • Sorry for delay in response... I was extremely tired yesterday & I had to go to sleep after posting the question. Anyway, I will check the alarm manager example and I will communicate back my finding. Thank You. – user836026 Jul 24 '12 at 06:53
  • I found also the Alarm exmaple on this link very helpful. https://github.com/commonsguy/cw-advandroid/tree/master/SystemServices/Alarm – user836026 Jul 24 '12 at 21:47
  • One other tip. You might want send a stickyBroadcast. sendStickyBroadcast(intent) when you catch the alarm. These broadcasts stick around even if the service goes down. You an then register a null receiver on the broadcast to check what its last value was. Possibly could store info in SharedPreferences as well. – Code Droid Jul 24 '12 at 21:53

3 Answers3

2

One possible way is to check out the AlarmManager. You can schedule your app to be run on an interval. You can also determine that perhaps I did not change since the last hour and lengthen the delay to extend battery life.

AlarmManager Documentation

Kaediil
  • 5,465
  • 2
  • 21
  • 20
2

Yes... Search for AlarmManager.. That is exactly what you need. You can schedule intent to be called and therefore start your service every hour.

simekadam
  • 7,334
  • 11
  • 56
  • 79
2

AlarmManager, as mentioned by the other posts, is what you're looking for.

However, It's against Android Best Practices to run things "forever, every hour" or so. If you start off by thinking "every hour, every day", you might take actions that would harm the user's battery life.

Turning on gps every hour, waiting for it to acquire a lock, then turning it off, uses a lot more battery life than if you were "smarter" while checking location.

If you do it right, e.g. setInexactRepeating, try to use WiFi location, set listeners for location update, then you can have your cake (long battery life for the user) and eat it too (still get the information you need).

For some good tips about how to keep your app from taking up too much battery (and possibly getting uninstalled), take a look at this video from GOogle IO 2012: http://www.youtube.com/watch?v=PwC1OlJo5VM

EDIT: Take a look at this, too: http://developer.android.com/reference/android/location/LocationListener.html

You can set a listener that only gets called when the device's location is changed. YMMV, but take a look.

Eagle
  • 2,046
  • 19
  • 25
  • Thanks Eagle ... I also found an example for Alarm Manager at http://stackoverflow.com/questions/4459058/alarm-manager-example – user836026 Jul 24 '12 at 21:49