From 974540cb774f44834697775998e65b745c962b6f Mon Sep 17 00:00:00 2001
From: William French
This lesson requires only coarse location. Request this permission with the - {@code uses-permission} element in your app manifest, as shown in the - following example: + {@code uses-permission} element in your app manifest, as the following code + snippet shows:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" @@ -92,18 +92,15 @@ trainingnavtop=trueIn your activity's {@link android.app.Activity#onCreate onCreate()} method, - create an instance of Google API Client using - {@code GoogleApiClient.Builder}. - Use the builder to add the - {@code LocationServices} - API.
- -The sample app defines a {@code buildGoogleApiClient()} method, called from - the activity's {@link android.app.Activity#onCreate onCreate()} method, - which includes the following code.
+ create an instance of Google API Client, using the + {@code GoogleApiClient.Builder} class to add the + + {@code LocationServices} + API, as the following code snippet shows.-protected synchronized void buildGoogleApiClient() { +// Create an instance of GoogleAPIClient. +if (mGoogleApiClient == null) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) @@ -112,6 +109,31 @@ protected synchronized void buildGoogleApiClient() { }+To connect, call + + {@code connect()} + from the activity's + {@code onStart()} + method. To disconnect, call + + {@code disconnect()} from the activity's + + {@code onStop()} method. The following snippet shows an example of how + to use both of these methods. +
+ ++protected void onStart() { + mGoogleApiClient.connect(); + super.onStart(); +} + +protected void onStop() { + mGoogleApiClient.disconnect(); + super.onStop(); +} ++Get the Last Known Location
Once you have connected to Google Play services and the location services @@ -130,7 +152,7 @@ protected synchronized void buildGoogleApiClient() { object. Do this in the {@code onConnected()} callback provided by Google API Client, which is called when the client is - ready. The following code sample illustrates the request and a simple + ready. The following code snippet illustrates the request and a simple handling of the response: