LocationSettingsRequest returns 0 to onActivityResult even if ok is clicked
Asked Answered
B

1

26

I am using following code to show popup to turn on location

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    SettingsClient client = LocationServices.getSettingsClient(getActivity());
    Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

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

    task.addOnFailureListener(getActivity(), new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            int statusCode = ((ApiException) e).getStatusCode();
            Log.d("AMOD", "onFailure " + statusCode);
            switch (statusCode) {
                case CommonStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    try {

                        ResolvableApiException resolvable = (ResolvableApiException) e;
                        resolvable.startResolutionForResult(getActivity(),
                                REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                        // Ignore the error.
                    }
                    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;
            }
        }
    });

Following is code for It showing popup as below enter image description here

But sometimes even if user clicks Ok I am getting resposne 0 i.e RESULT_CANCEL this happening after updating play services to 16.0.0

Reported this bug on google issuetracker as well following is link for more details on it

https://issuetracker.google.com/issues/118347902

Bipetalous answered 24/10, 2018 at 9:46 Comment(3)
please post the code that you are using to receive the result of the popupWrangler
do you call super in onActivityResult ? if so try to remove it and let me know if that solves your issueMincey
I have the same problem, I found that some times it came to onActivityResult and start NETWORK_PROVIDER && GPS_PROVIDER with result OK, and some times it result canceled and start device only location mode. this is my question #53388241Worrywart
L
0

I just tried about 50 times to reproduce this and I always got code -1 (Pixel 3 running Android Pie). But I get 0 about half the time on my Nexus 5x running Oreo.

I can't isolate exactly what you're doing wrong, but here's a sample project I made that does the same thing and works correctly every time:

https://github.com/gavingt/basic_location_improved

Legible answered 31/10, 2018 at 3:18 Comment(4)
Using support library version 16.0.0 and yes issue occurring only on android N and lower os versionsBipetalous
Why is your app's targetSDKVersion so low? It's clearly less than 23 because it's not requesting runtime permissions. This could have something to do with it. Either way, you should just start with my code as a baseline because something you're doing isn't working and mine works great.Legible
It is just demo app since I don't have enough time to handle permission and since its just a sample kept target 23. But tried with sdk 27 and enabled permission from settings issue still occursBipetalous
Without the full project files, it's hard to tell what the problem is. But I would guess it's something to do with your Gradle dependencies.Legible

© 2022 - 2024 — McMap. All rights reserved.