Using Google Play Services
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" />
...
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.
Using Google Play Services
To use Play Services, you need to create a client. That client is an instance of the GoogleApiClient
class. You can find the documentation for this class (and all the other Play Services classes you will
be using in these two chapters) in the Play Services reference section at developer.android.com/
reference/gms-packages.html.
To create a client, create a GoogleApiClient.Builder and configure it. At a minimum, you want
to configure the instance with the specific APIs you will be using. Then call build() to create an
instance.
Inside your onCreate(Bundle), create an instance of GoogleApiClient.Builder and add the Location
Services API to your instance.
Listing 33.8 Creating GoogleApiClient (LocatrFragment.java)
public class LocatrFragment extends Fragment {
private ImageView mImageView;
private GoogleApiClient mClient;
public static LocatrFragment newInstance() {
return new LocatrFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.build();
}