Setting Up Google Play Services

To get your location using the Fused Location Provider, you need to use Google Play Services. To get those up and running, you will need to add a few standard bits of boilerplate to your app.

First, you need to add the Google Play Services library dependency. The services themselves live in the Play Store app, but the Play Services library contains all the code to interface with them.

Open up your app module’s settings (FileProject Structure). Navigate to its dependencies and add a library dependency. Type in the following dependency name: com.google.android.gms:play-services-location:10.0.1. (As of this writing, this dependency will not show up in search results, so type carefully.) This is the location portion of Play Services.

Over time, the version number for this library will change. If you want to see what the most up-to-date version is, search the library dependencies for play-services. The com.google.android.gms:play-services dependency will appear, along with a version number. This is the dependency that includes everything in Play Services. If you want to use the latest version of the library, you can use the version number from play-services for the more limited play-services-location library, too.

Which version number should you use, though? In your own practice, it is best to use the most recent version you possibly can. But we cannot guarantee that the code in this book will work the same for future versions. So for Locatr, use the version we wrote this code for: 10.0.1.

Next, you need to verify that Play Services is available. Since the working parts live in another app on your device, the Play Services library is not always guaranteed to be working. The library makes it easy to verify this. Update your main activity to perform this check.

Listing 33.6  Adding Play Services check (LocatrActivity.java)

public class LocatrActivity extends SingleFragmentActivity {
    private static final int REQUEST_ERROR = 0;

    @Override
    protected Fragment createFragment() {
        return LocatrFragment.newInstance();
    }

    @Override
    protected void onResume() {
        super.onResume();

        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int errorCode = apiAvailability.isGooglePlayServicesAvailable(this);

        if (errorCode != ConnectionResult.SUCCESS) {
            Dialog errorDialog = apiAvailability
                    .getErrorDialog(this, errorCode, REQUEST_ERROR,
                            new DialogInterface.OnCancelListener() {

                                @Override
                                public void onCancel(DialogInterface dialog) {
                                    // Leave if services are unavailable.
                                    finish();
                                }
                            });

            errorDialog.show();
        }
    }
}

Normally you would not use a bare Dialog like this. However, in this case you will receive the same errorCode every time LocatrActivity starts up, so the Dialog will always be displayed correctly.

Location permissions

You will also need some location permissions for your app to work. There are two relevant permissions: android.permission.ACCESS_FINE_LOCATION and android.permission.ACCESS_COARSE_LOCATION. Fine location is the GPS radio; coarse location is derived from cell towers or WiFi access points.

In this chapter, you will be requesting a high-accuracy location fix, so you will definitely need ACCESS_FINE_LOCATION. But it is also a good idea to request ACCESS_COARSE_LOCATION. If the fine location provider is not available, this gives you permission to use the coarse provider as a backup.

Add these permissions to your manifest. Add an internet permission while you are at it, so that you can query Flickr.

Listing 33.7  Adding permissions (AndroidManifest.xml)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bignerdranch.android.locatr" >

    <uses-permission
        android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission
        android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission
        android:name="android.permission.INTERNET" />
    ...
</manifest>

Both FINE_LOCATION and COARSE_LOCATION are dangerous permissions. That means that, unlike INTERNET, this manifest code is not enough: You must also make a runtime request to use these locations.

To handle that, you will need to write some more permissions code later in this chapter. For now, though, you will move on with integrating Google Play Services.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.104.173