3

I have an android application that needs to ascertain its current location (Using Google Play Services LocationClient) and then send that location back to a server. This needs to happen in the background at regular intervals e.g. Every 5 minutes

This code is working very well and it generally delivers the locations every 5 minutes within a few seconds tolerance. The problem occurs when android can not get a fix. Under these conditions I was expecting the callback to be run after 5 minutes with a NULL location object, however this does not happen. The Application merely continues to look for locations without ever calling the callback.

I call my LocationClient's requestLocationUpdates method with a locationRequest object created as follows

locationrequest = LocationRequest.create();
locationrequest.setInterval(5 * 60 * 1000);  
locationrequest.setFastestInterval(3 * 60 * 1000);
locationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationrequest.setExpirationDuration(45 *1000);
locationclient.requestLocationUpdates(locationrequest, mPendingIntent);

I can not see a suitable way to have the LocationClient call me every 5 minutes, even when it is not able to get a location. I appreciate that I could have an alarm that goes off every 5 minutes and then sends the most recent location back to the server but I was hoping to handle the sending within the callback.

Has anyone else had to handle this situation, or have any idea how to address it?

Steve Weet
  • 28,126
  • 11
  • 70
  • 86
  • 2
    Remember you are using `locationclient.requestLocationUpdates...` as in, you will get notified of an update, but if the device can not get a location it will not update and hence not notify. You might want to try a different approach, by running a timer or something similar. Per documentation: `It may take a while to receive the first location update. If an immediate location is required, applications may use the getLastKnownLocation(String) method.` – blindstuff Aug 30 '13 at 14:45
  • I had a problem with my 4.0 phone taking forever for a location and only when I switched off (manually) the use assisted gps did it function within acceptable parameters. – danny117 Aug 30 '13 at 17:05

1 Answers1

0

I don't know what you're doing in the rest of the code but I think you just need to manage your LocationRequest and LocationClient in relation to the lifecycle methods of the Activity or Fragment you are calling them from.

The receiving location updates tutorial along with this post from the android developers blog are good examples.

Since your app must be connected to LocationClient to recieve updates if you switch these updates on and off during say onResume() or onCreate() and onStop() you effectivly only run the timer when you app is in the foreground. Then, as the developer blog post suggests, when your app is in the background you can switch over to a passive location listener that can still recieve updates but does not user the same timer based approach. When your app comes back to the foreground you can turn off the passive listener, reconnect to Google Play Services and start your 5 minute timer again.

Rarw
  • 7,645
  • 3
  • 28
  • 46
  • The problem is that the app is rarely in the foreground. It is generally in the background collecting location data and sending it back to the server. This is all working fine the only problem is that it does not call me back when it fails to get a location. – Steve Weet Aug 30 '13 at 14:43
  • I understand. But as I understand the LocationClient, once you lose connection to GooglePlayServices there can be no location updates. In that case there's no need for the call back to be triggered every time the timer runs (e.g. send back a null) because once you recieve notification via the connection lost callback the 5 minute timer effecitvly becomes irrelivant. What I would do is either (1) set a boolean flag that I check with a timer I start once the connection to GooglePlay is lost or (2) use a BroadcastReceiver – Rarw Aug 30 '13 at 15:00
  • I'm not losing connection to GooglePlayServices. I have the connection but the device can not get a location so it doesn't inform me. As @blindstuff says above the problem is that the location hasn't changed so there is no reason to call my callback. I think I need to have an AlarmManager fireup every 5 minutes and get the latest location which it can then send to the server. – Steve Weet Aug 30 '13 at 16:26