Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Location Settings have failed when changed

I am creating a locationrequest object of the LocationRequest class whose methods are used to determine the level of location accuracy my app needs.

private LocationRequest mLocationRequest;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(2000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

and then creating a LocationSettingsRequest.Builder object and then adding the location request object to it.

new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);

According to the Android documentation, SettingsClient is responsible to ensure that the device's system settings are properly configured for the app's location needs.

SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = 
client.checkLocationSettings(builder.build());

The documentation states that when the Task completes, the client can check the location settings by looking at the status code from the LocationSettingsResponse object.

task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() 
{
            @Override
            public void onComplete(Task<LocationSettingsResponse> task) {
                try {
                    LocationSettingsResponse response = task.getResult(ApiException.class);

                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                } catch (ApiException exception) {
                    Log.v(" Failed ", String.valueOf(exception.getStatusCode()));

                    switch (exception.getStatusCode()) {

                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            // Location settings are not satisfied. But could be fixed by showing the
                            // user a dialog.
                            // Cast to a resolvable exception.
                            ResolvableApiException resolvable = (ResolvableApiException) exception;
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            try {
                                resolvable.startResolutionForResult(
                                        MapsActivity.this,
                                        REQUEST_CHECK_SETTINGS);
                            } 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;
                    }
                }
            }
        });

According to from what I understand the code above checks if the task on completed was able to accept the location settings that were changed when we did

The documentation also states that if the status code is RESOLUTION_REQUIRED, the client can call startResolutionForResult(Activity, int) to bring up a dialog, asking for the user's permission to modify the location settings to satisfy those requests.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
    switch (requestCode) {
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    // All required changes were successfully made
                    Toast.makeText(getBaseContext(), "All good", Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    // The user was asked to change settings, but chose not to
                    break;
                default:
                    break;
            }
            break;
    }
}

I want this scenario to occur wherein I can test if RESOLUTION_REQUIRED was the status code and I can prompt the user to change it. Can anyone please tell me what can I do in the code or my phone setting to test this scenario.

like image 854
Vinit Singh Avatar asked Nov 08 '22 10:11

Vinit Singh


1 Answers

Vinit Singh,

I dont' know if my answer it's too late but i was having this exception when my device location is disabled.

I receive this excpetion:

com.google.android.gms.common.api.ResolvableApiException: 6: RESOLUTION_REQUIRED

Hope it helps some way.

like image 106
Apolo Avatar answered Nov 24 '22 12:11

Apolo