Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Setting Up Google Play Services


Now hook it up in LocatrActivity. Delete everything inside this class and replace it, like so:


Listing 33.5  Hooking up Locatr fragment (LocatrActivity.java)


public class LocatrActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return LocatrFragment.newInstance();
}
}


With that, you should be ready to get into some trouble.


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 (File → Project 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.

Free download pdf