Follow the instructions below.
1) Create a LocationRequest according to your desire
LocationRequest mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(10 * 1000) .setFastestInterval(1 * 1000);
2) Create LocationSettingsRequest.Builder
LocationSettingsRequest.Builder settingsBuilder = new LocationSettingsRequest.Builder() .addLocationRequest(mLocationRequest); settingsBuilder.setAlwaysShow(true);
3) Get the LocationSettingsResponse Task using the following code
Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(this) .checkLocationSettings(settingsBuilder.build());
Note: LocationServices.SettingsApi deprecated, so use SettingsClient instead.
4) Add OnCompleteListener to get the result from the Task completion. Task client can check the location parameters by looking at the status code from the LocationSettingsResponse object.
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() { @Override public void onComplete(@NonNull Task<LocationSettingsResponse> task) { try { LocationSettingsResponse response = task.getResult(ApiException.class); } catch (ApiException ex) { switch (ex.getStatusCode()) { case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { ResolvableApiException resolvableApiException = (ResolvableApiException) ex; resolvableApiException .startResolutionForResult(MapsActivity.this, LOCATION_SETTINGS_REQUEST); } catch (IntentSender.SendIntentException e) { } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: break; } } } });
Case 1: LocationSettingsStatusCodes.RESOLUTION_REQUIRED : Location is not included, but we can ask the user to enable the location by prompting him to enable the location in the dialog box (by calling startResolutionForResult ).

EXAMPLE 2: LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE : LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE settings failed. However, we do not have the opportunity to fix the settings, so we will not show the dialog.
5) OnActivityResult we can get the user action in the location settings dialog. RESULT_OK => User has enabled Location. RESULT_CANCELLED - The user rejected the location setup request.
RevanthKrishnaKumar V. Jan 18 '18 at 17:06 2018-01-18 17:06
source share