16
16
17
17
package com .google .android .gms .location .sample .basiclocationsample ;
18
18
19
+ import android .Manifest ;
19
20
import android .content .Intent ;
20
21
import android .content .pm .PackageManager ;
21
22
import android .location .Location ;
22
23
import android .net .Uri ;
23
24
import android .os .Bundle ;
24
25
import android .provider .Settings ;
26
+ import android .support .annotation .NonNull ;
25
27
import android .support .design .widget .Snackbar ;
28
+ import android .support .v4 .app .ActivityCompat ;
26
29
import android .support .v7 .app .AppCompatActivity ;
27
30
import android .util .Log ;
28
31
import android .view .View ;
29
32
import android .widget .TextView ;
30
- import android .widget .Toast ;
31
33
32
- import com .google .android .gms .common .ConnectionResult ;
33
- import com .google .android .gms .common .api .GoogleApiClient ;
34
- import com .google .android .gms .common .api .GoogleApiClient .ConnectionCallbacks ;
35
- import com .google .android .gms .common .api .GoogleApiClient .OnConnectionFailedListener ;
34
+ import com .google .android .gms .location .FusedLocationProviderClient ;
36
35
import com .google .android .gms .location .LocationServices ;
37
-
38
- import android .support .annotation .NonNull ;
39
- import android .support .v4 .app .ActivityCompat ;
40
- import android .Manifest ;
36
+ import com .google .android .gms .tasks .OnCompleteListener ;
37
+ import com .google .android .gms .tasks .Task ;
41
38
42
39
import java .util .Locale ;
43
40
44
41
/**
45
42
* Location sample.
46
43
* <p>
47
44
* Demonstrates use of the Location API to retrieve the last known location for a device.
48
- * This sample uses Google Play services (GoogleApiClient) but does not need to authenticate a user.
49
- * See https://github.com/googlesamples/android-google-accounts/tree/master/QuickStart if you are
50
- * also using APIs that need authentication.
51
45
*/
52
- public class MainActivity extends AppCompatActivity implements
53
- ConnectionCallbacks , OnConnectionFailedListener {
46
+ public class MainActivity extends AppCompatActivity {
54
47
55
48
private static final String TAG = MainActivity .class .getSimpleName ();
56
49
57
50
private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34 ;
58
51
59
52
/**
60
- * Provides the entry point to Google Play services .
53
+ * Provides the entry point to the Fused Location Provider API .
61
54
*/
62
- private GoogleApiClient mGoogleApiClient ;
55
+ private FusedLocationProviderClient mFusedLocationClient ;
63
56
64
57
/**
65
58
* Represents a geographical location.
@@ -82,21 +75,19 @@ public void onCreate(Bundle savedInstanceState) {
82
75
mLatitudeText = (TextView ) findViewById ((R .id .latitude_text ));
83
76
mLongitudeText = (TextView ) findViewById ((R .id .longitude_text ));
84
77
85
- buildGoogleApiClient ( );
78
+ mFusedLocationClient = LocationServices . getFusedLocationProviderClient ( this );
86
79
}
87
80
88
- /**
89
- * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
90
- */
91
- private synchronized void buildGoogleApiClient () {
92
- mGoogleApiClient = new GoogleApiClient .Builder (this )
93
- .enableAutoManage (this , this )
94
- .addConnectionCallbacks (this )
95
- .addOnConnectionFailedListener (this )
96
- .addApi (LocationServices .API )
97
- .build ();
98
- }
81
+ @ Override
82
+ public void onStart () {
83
+ super .onStart ();
99
84
85
+ if (!checkPermissions ()) {
86
+ requestPermissions ();
87
+ } else {
88
+ getLastLocation ();
89
+ }
90
+ }
100
91
101
92
/**
102
93
* Provides a simple way of getting a device's location and is well suited for
@@ -106,43 +97,27 @@ private synchronized void buildGoogleApiClient() {
106
97
* <p>
107
98
* Note: this method should be called after location permission has been granted.
108
99
*/
100
+ @ SuppressWarnings ("MissingPermission" )
109
101
private void getLastLocation () {
110
- //noinspection MissingPermission
111
- mLastLocation = LocationServices .FusedLocationApi .getLastLocation (mGoogleApiClient );
112
-
113
- if (mLastLocation != null ) {
114
- mLatitudeText .setText (String .format (Locale .ENGLISH , "%s: %f" , mLatitudeLabel ,
115
- mLastLocation .getLatitude ()));
116
- mLongitudeText .setText (String .format (Locale .ENGLISH , "%s: %f" , mLongitudeLabel ,
117
- mLastLocation .getLongitude ()));
118
- } else {
119
- Toast .makeText (this , R .string .no_location_detected , Toast .LENGTH_LONG ).show ();
120
- }
121
- }
122
-
123
- /**
124
- * Runs when a GoogleApiClient object successfully connects.
125
- */
126
- @ Override
127
- public void onConnected (Bundle connectionHint ) {
128
- if (!checkPermissions ()) {
129
- requestPermissions ();
130
- }
131
- }
132
-
133
- @ Override
134
- public void onConnectionFailed (@ NonNull ConnectionResult connectionResult ) {
135
- final String text = "Exception while connecting to Google Play services" ;
136
- Log .w (TAG , text + ": " + connectionResult .getErrorMessage ());
137
- showSnackbar (text );
138
- }
139
-
140
-
141
- @ Override
142
- public void onConnectionSuspended (int cause ) {
143
- final String text = "Connection suspended" ;
144
- Log .w (TAG , text + ": Error code: " + cause );
145
- showSnackbar ("Connection suspended" );
102
+ mFusedLocationClient .getLastLocation ()
103
+ .addOnCompleteListener (this , new OnCompleteListener <Location >() {
104
+ @ Override
105
+ public void onComplete (@ NonNull Task <Location > task ) {
106
+ if (task .isSuccessful () && task .getResult () != null ) {
107
+ mLastLocation = task .getResult ();
108
+
109
+ mLatitudeText .setText (String .format (Locale .ENGLISH , "%s: %f" ,
110
+ mLatitudeLabel ,
111
+ mLastLocation .getLatitude ()));
112
+ mLongitudeText .setText (String .format (Locale .ENGLISH , "%s: %f" ,
113
+ mLongitudeLabel ,
114
+ mLastLocation .getLongitude ()));
115
+ } else {
116
+ Log .w (TAG , "getLastLocation:exception" , task .getException ());
117
+ showSnackbar (getString (R .string .no_location_detected ));
118
+ }
119
+ }
120
+ });
146
121
}
147
122
148
123
/**
@@ -177,10 +152,16 @@ private void showSnackbar(final int mainTextStringId, final int actionStringId,
177
152
*/
178
153
private boolean checkPermissions () {
179
154
int permissionState = ActivityCompat .checkSelfPermission (this ,
180
- Manifest .permission .ACCESS_FINE_LOCATION );
155
+ Manifest .permission .ACCESS_COARSE_LOCATION );
181
156
return permissionState == PackageManager .PERMISSION_GRANTED ;
182
157
}
183
158
159
+ private void startLocationPermissionRequest () {
160
+ ActivityCompat .requestPermissions (MainActivity .this ,
161
+ new String []{Manifest .permission .ACCESS_FINE_LOCATION },
162
+ REQUEST_PERMISSIONS_REQUEST_CODE );
163
+ }
164
+
184
165
private void requestPermissions () {
185
166
boolean shouldProvideRationale =
186
167
ActivityCompat .shouldShowRequestPermissionRationale (this ,
@@ -196,9 +177,7 @@ private void requestPermissions() {
196
177
@ Override
197
178
public void onClick (View view ) {
198
179
// Request permission
199
- ActivityCompat .requestPermissions (MainActivity .this ,
200
- new String []{Manifest .permission .ACCESS_FINE_LOCATION },
201
- REQUEST_PERMISSIONS_REQUEST_CODE );
180
+ startLocationPermissionRequest ();
202
181
}
203
182
});
204
183
@@ -207,9 +186,7 @@ public void onClick(View view) {
207
186
// Request permission. It's possible this can be auto answered if device policy
208
187
// sets the permission in a given state or the user denied the permission
209
188
// previously and checked "Never ask again".
210
- ActivityCompat .requestPermissions (MainActivity .this ,
211
- new String []{Manifest .permission .ACCESS_COARSE_LOCATION },
212
- REQUEST_PERMISSIONS_REQUEST_CODE );
189
+ startLocationPermissionRequest ();
213
190
}
214
191
}
215
192
@@ -258,4 +235,4 @@ public void onClick(View view) {
258
235
}
259
236
}
260
237
}
261
- }
238
+ }
0 commit comments