1

my app is dependent on GPS signal, so I am asking user at app startup to enable GPS. However if user says no, app continues running.

GPS is crucial to my app so I would like to display the dialog again if user says no. If it is not possible, just close the app if user doesn't grant permission.

Could you please help?

Thanks a lot.

 protected void createLocationRequest() {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

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

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

    task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            // All location settings are satisfied. The client can initialize
            // location requests here.
            // ...
        }
    });

    task.addOnFailureListener(this, new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            if (e instanceof ResolvableApiException) {
                // Location settings are not satisfied, but this can be fixed
                // by showing the user a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    ResolvableApiException resolvable = (ResolvableApiException) e;
                    resolvable.startResolutionForResult(MainActivity.this,
                            1);
                } catch (IntentSender.SendIntentException sendEx) {
                    // Ignore the error.
                }
            }
        }
    });
}
Eduard Bakeš
  • 176
  • 2
  • 11
  • You can always just call the request for permissions again. Nothing prevents that. It might be nice to pop up a dialog that explains that without permissions the app can't function. – Gabe Sechan Apr 19 '18 at 15:55
  • Yes, but GPS is really essential and it is used all the time the app runs. So is there way to show dialog again or close app if user doesn't allow turn on GPS? – Eduard Bakeš Apr 19 '18 at 15:59
  • If you want to reduce the boilerplate, I can suggest you to take a look at this library: https://github.com/tbruyelle/RxPermissions. I'm using it in production and it works like a charm. – Nicola Gallazzi Apr 19 '18 at 16:00
  • How about calling the startResolutionForResult again in the onActivityResult when it failed, by starting a new thread, to prevent function call's stack overflow? – KYHSGeekCode Apr 19 '18 at 16:00
  • Why dont you just use `ContextCompat.checkSelfPermission` to check the permission and prevent the user from doing anything is its not granted – tyczj Apr 19 '18 at 16:08
  • KYHSGeekCode good tip, I have managed to get it working. Posting the solution. Thanks. – Eduard Bakeš Apr 19 '18 at 16:13

2 Answers2

1

Create a Listener that listen for the mode change on device GPS, in your app base just implement the listener. Then handle the functions accordingly. if GPS is enabled, the app keeps running, else show an alert box requesting the user to enable gps, since all ur activity extends from ur app base.

You check out the answer here on how to find out if GPS is enabled.

You can also check out this tutorial here on how to create a listener.

Thankgod Richard
  • 507
  • 6
  • 16
0

Solution is to override onActivityResult method and handle it with matching requestCode.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == 6) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }else{
            createLocationRequest();
        }
    }
}
Eduard Bakeš
  • 176
  • 2
  • 11