androidlocationandroid-gpsgoogle-location-services

LocationSettingsRequest dialog to enable GPS


From past few days I am looking for a solution but unable to find any. I am working on a situation when the google location accuracy is toggled off for android 9+ then the application should prompt user to toggle on the location accuracy.

Problem facing:

Is it possible if (statusCode == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) then navigate directly to google location accuracy screen of the device(which looks difficult). OR customise the dialog box which is prompted by google services library (which is not possible i assume). Any thoughts would be appreciated.

Below is the snippet.

mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(UPDATE_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();     mSettingsClient.checkLocationSettings(mLocationSettingsRequest).addOnSuccessListener(mActivity,
        new OnSuccessListener<LocationSettingsResponse>() {
    @Override
    public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
        mFusedProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {


            }
        });
    }
}).addOnFailureListener(mActivity, new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        int statusCode = ((ApiException) e).getStatusCode();
        if(statusCode == LocationSettingsStatusCodes.RESOLUTION_REQUIRED){
            //Location settings not satisfied.
            try {

                ResolvableApiException resolvable = (ResolvableApiException) e;
                resolvable.startResolutionForResult(
                        mActivity,
                        LOCATION_REQUEST);
            } catch (IntentSender.SendIntentException ex) {
    
            }
        } else {

        }

    }
});
 

Solution

  • Try this,works for me

    private void showLocationDialog() {
        new AlertDialog.Builder(RequestLocationUpdatesWithIntentActivity.this).setTitle("The location service is not enabled on the phone.")
                .setMessage("Go to Settings > Location information (enable location service).")
                .setNegativeButton("cancels", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                })
                .setPositiveButton("Unset", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        try {
                            RequestLocationUpdatesWithIntentActivity.this.startActivity(intent);
                        } catch (ActivityNotFoundException ex) {
                            intent.setAction(Settings.ACTION_SETTINGS);
                            try {
                                RequestLocationUpdatesWithIntentActivity.this.startActivity(intent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                })
                .show();
    }
    

    You can call it here. enter image description here

    And then there's this effect.

    enter image description here