This blog is about how to show Popup for enable Location setting.
1. Add below dependency to app gradle file.
compile 'com.google.android.gms:play-services:9.6.1'
2. Implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener into your fragment/activity.
3. Create Instance of GoogleApiClient.
private GoogleApiClient mGoogleApiClient;
4. initialize GoogleApiClient.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
5. Create LocationRequest.
LocationRequest locationRequest = LocationRequest.create()
.setInterval(10 * 60 * 1000) // every 10 minutes
.setExpirationDuration(10 * 1000) // After 10 seconds
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder;
builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
// set builder to always true (Shows the dialog after never operation too)
builder.setAlwaysShow(true);
6. Create Pending Request.
PendingResult result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
7. Set Callback to result.
result.setResultCallback(new ResultCallback() {
@Override
public void onResult(Result result) {
final Status status = result.getStatus();
// final LocationSettingsStates state = result.getStatus().ge
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
status.startResolutionForResult(SplashActivity.this,100);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
});
8. Add Location permission in manifest.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
*Make sure you have implement request permission for Marshmallow and above OS.
Happy Coding :D
0 Comment(s)