Android: How to Update Your Code from GooglePlayServicesClient to GoogleApiClient

If you have been following the development of Android in the last few months, you noticed that Google pushes to separate the monolithic Android system into a lot of smaller components. While this is great for the end users and security reasons, it raises a few question marks for us developers from time to time. One example is the change in how to request location updates for the Android device. This post will guide you through the necessary steps.

Previous Code Using GooglePlayServicesClient

This is a shortened example of how an app could use the GooglePlayServicesClient to request the GPS location of a device.

public class LocationMonitoringService extends Service implements  
        GooglePlayServicesClient.ConnectionCallbacks {

    LocationClient mLocationClient;

    ...

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        ...

        mLocationClient = new LocationClient(this, this, this);
        mLocationClient.connect();

        return START_STICKY;
    }

    /*
     * LOCATION CALLBACKS
     */
    @Override
    public void onConnected(Bundle dataBundle) {
        mLocationClient.requestLocationUpdates(mLocationRequest, locationListener);
    }

    /*
     * Called by Location Services if the connection to the
     * location client drops because of an error.
     */
    @Override
    public void onDisconnected() {}
}

New Code Using GoogleApiClient

Let's look at a few pieces we've to adjust to make the app code state-of-the-art again.

Implements

The first element, which needs to be updated, is the interface the activity or service is implementing. Thus replace

public class LocationMonitoringService extends Service implements GooglePlayServicesClient.ConnectionCallbacks  

with

public class LocationMonitoringService extends Service implements GoogleApiClient.ConnectionCallbacks  

Android Studio should offer you the necessary imports.

Callbacks

It should also display you an error. In particular that you're not implementing the necessary callbacks anymore. A closer look reveals that the

@Override
public void onDisconnected() {}  

changed to

@Override
public void onConnectionSuspended(int i) {}  

Theoretically, your code should compile again, but it isn't functional yet. You'll also need to update the way you request the location updates itself.

Constructor

In order to achieve that, change the type and constructor of your mLocationClient. Previously it was a regular class constructor.

LocationClient mLocationClient;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {  
    mLocationClient = new LocationClient(this, this, this);
    mLocationClient.connect();
}

The GoogleApiClient approaches it in a Builder pattern:

GoogleApiClient mLocationClient;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {  
    mLocationClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    mLocationClient.connect();
}

Please note the .addApi(LocationServices.API) line. This gives you the opportunity to add multiple services in one call using the same listeners! The connect() method is still the same.

Request Code

Lastly, the way you finally have to set the listener is a little different as well. Previously it simply was a method call on the mLocationClient object:

mLocationClient.requestLocationUpdates(mLocationRequest, locationListener);  

Now it is:

LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, locationListener);  

Notice that the method call is static and you have to add mLocationClient as first parameter. The way you build mLocationRequest and the locationListener is the same and can be found in the Android Docs.

Summary

This post showed you the essentials how to update your code to the GoogleApiClient API for location tracking, activity monitoring and other device APIs. Staying up-to-date is important to not lose speed in the fast world of mobile development.

We hope this was helpful to you and made you want to update your app code as well. Let us know if you encounter any problems at @futurestud_io.

Explore the Library

Find interesting tutorials and solutions for your problems.