Using the New Google Play Service API

Posted on 17 Apr 2015 by Eric Oestrich

I recently got to use the new FusedLocationApi from Google Play Services on Android. This is something I have tried in the past, but all of the Android documentation refers to the deprecated LocationClient. I wanted to figure out how to use the new API so I did not need to update this project again.

public class MainActivity extends Activity implements
  GoogleApiClient.OnConnectionFailedListener {

  private GoogleApiClient mGoogleApiClient;

  Button mLocationUpdates;

  public void onCreate(Bundle savedInstanceState) {

    mGoogleApiClient = new GoogleApiClient.Builder(this)

    mLocationUpdates = (Button) findViewById(;
    // Disable until connected to GoogleApiClient

  public void onConnected(Bundle bundle) {
    mLocationUpdates.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {

  private void locationRequest() {
    LocationRequest request = new LocationRequest()
      .setInterval(1000) // Every 1 seconds
      .setExpirationDuration(60 * 1000) // Next 60 seconds

    Intent intent = new Intent(this, LocationUpdateService.class);
    PendingIntent pendingIntent = PendingIntent
      .getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      .requestLocationUpdates(mGoogleApiClient, request, pendingIntent);

This starts by creating a GoogleApiClient and setting connection callbacks to the activity. Once the GoogleApiClient is connected it calls onConnected and we enable the button that will start tracking location. I used the IntentService of receiving updates for this as it was simpler and worked for what I wanted. We create a LocationRequest whenever the button is pressed and ask for updates every second for 60 seconds.

public class LocationUpdateService extends IntentService {
  public LocationUpdateService() {

  protected void onHandleIntent(Intent intent) {
    Bundle bundle = intent.getExtras();

    if (bundle == null) {

    Location location = bundle.getParcelable("");

    if (location == null) {

    // Deal with new location

The LocationUpdateService is very simple and pulls the new updates out of the Intent it gets passed.

Take Aways

I liked doing the IntentService route since we didn’t have to worry about the activity being killed to keep receiving updates to location. It also got around the Android 5.1 “feature” of knocking all alarms to a 60 second interval no matter how small of a frequency you actually requested (to fetch getLastLocation() from the LocationClient as I had previously used.)

comments powered by Disqus
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.