0

My application is based on Google Map and Location which includes client-server communications. Apart from all that, periodically I want to update the user location to web server. I gave thought myself and ended up with below two scenarios,

First One : I use Service class to fetch user location periodically (considering 2-3 hours once) and update to server. Ideally I want to avoid this! Because just to upload user location its not good to run the service all the time is what I feel.

Second Thought : I can use IntentService to fetch the user location periodically (considering same 2-3 hours) and update to web server. Here IntentService will be invoked by an AlarmManager. That is every 2-3 hours once alarm manager will start IntentService.

But I feel using AlarmManager for 2-3 hours once is not a good idea too.

I want to understand which one will be efficient and more relevant! Whether this scenario can be implemented in any other approach? If so help me do it!

Thanks in advance...

Kavin Prabhu
  • 2,307
  • 2
  • 17
  • 36
  • 1
    "But I feel using AlarmManager for 2-3 hours once is not a good idea too" -- why? We cannot address your concerns unless you actually express your concerns. "I want to understand which one will be efficient" -- `AlarmManager`. Only have a service running when it is [actively delivering value to the user](https://commonsware.com/blog/2014/07/27/role-services.html). Watching the clock tick is not actively delivering value to the user. "Whether this scenario can be implemented in any other approach?" -- request location updates be delivered to you every 2-3 hours by `PendingIntent`. – CommonsWare Dec 12 '15 at 16:35
  • So, what I understood is, I have to use `AlarmManager` along with `PendingIntent`. The Pending Intent will send a broadcast to the receiver class, from there I have to fetch location and update to server! Am I right? – Kavin Prabhu Dec 12 '15 at 16:54
  • Only if you are going to use something like `getLastKnownLocation()` on `LocationManager`, which may return an out-of-date location or `null`. If you need to ensure that you get a current location fix, this will be much more complicated. Getting a location is asynchronous (as the device may have to turn on the GPS radio and wait for satellite data) and fraught with problems (e.g., user cannot get a GPS signal). You will wind up using a `WakefulBroadcastReceiver` coupled with a `Service`. – CommonsWare Dec 12 '15 at 17:10
  • Okie, after reading all possible solutions I have implemented using `PendingIntent`. In `BroadcastReceiver` class all the time I get location as `null. In `onReceive` method I used `Location location = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);` In SplashScreen I implemented `GoogleApiClient`, `ResultCallback` for location settings and `LocationListener`. In callbacks `onConnected(Bundle b)` I registered `PendingIntent`. Now how to get location instead null? – Kavin Prabhu Dec 12 '15 at 18:38

0 Answers0