Background Location Updates on Android

24 / Jul / 2014 by Himanshu Singh 3 comments

In my previous blog, I mentioned my first task about removing the AlarmManager/LocationManager approach we were using and replace it with new Location APIs that were announced at Google I/O last year.

Note: If you are unaware about using Google location api in the foreground, Please firstly read the previous blog.

This method was working well while the android application was in the foreground, but was not updating the location while in the background.

Confused, I went to the docs to find what was going wrong. I was using the LocationClient.requestLocationUpdates() version which takes in a LocationListener. The documentation states “This method is suited for the foreground use cases. For background use cases, the PendingIntent version of the method is recommended”.

WAY TO USE

1. Use the PendingIntent in our code.

[java]
@Override
public void onConnected(Bundle bundle) {
Intent intervalIntent = new Intent(this, IntervalPendingIntentService.class);
PendingIntent intervalPendingIntent = PendingIntent.getService(this, 1,intervalIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mLocationClient.requestLocationUpdates(mLocationRequest, intervalPendingIntent);

}
[/java]

2. We can get location in the started component like :

[java]
public class IntervalPendingIntentService extends IntentService{
@Override
protected void onHandleIntent(Intent intent)
{
Location location = intent.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);
if(location !=null)
{
//our location based code
}
}
}
[/java]

Note: android.location.Location class implements Parcelable interface by which we can send/receive whole Location object to/from any android component through Intent. e.g. :

[java]
Intent intent = new Intent();
intent.putExtra("loc", location);
getApplicationContext().sendBroadcast(intent);
[/java]

Changing the new API, we receive location updates even when the app is in the background. This makes the api code is more manageable and we also get an advantage of the API’s battery-saving optimization.

FOUND THIS USEFUL? SHARE IT

comments (3)

  1. Ghiyats Hanif

    Thanks for the post, android programming is a brand new thing to me, i’ve been playing with android’s location and i have a same issue, im going to send a periodic location updates to a server and i’m using the AlarmManager/LocationManager approach, set an alarmManager with a defined Interval then when the alarmReceiver is triggered it will get device’s current location (using locationManager) and send it to the server ont it onReceive method. i found out this FusedLocation as a great replacement as LocationManager give me an additional job to get the best location provider. is it possible to perform sending location updates periodically using fusedLocation api without an alarmManager? if so, how can i do that? thanks in advance!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *