Strange behavior of LocationRequest priorities

2019-02-21 01:19发布

问题:

So I have an app. It depends on user location. On lot of devices it worked fine, as I expect. There are problem with samsung s8, I checked 4 real s8 from different users and with different app that needs location, and the problem there. But now my xiaomi mi a1 (with clean android) get android security update (8.1 November) and I get same problem as on s8.

The problem: I turn off location service in settings of device. I start my app, or another app that need location (not google maps, it works awesome). I get default system dialog to turn on location services. I press OK, and one time it start location service with all sources and another time it starts gps only. The "GPS only" works very bad, I wait one - two minutes and it not give my position and not set blue dot on map. I turn off location and close app, open and again this dialog, and after "ok" it start all sources, and I get location instantly.

As I understand, the mode that will be set on start location service depends on priority of LocationRequest. So why once it set mode "all sources" and once "gps only", its about LocationRequest.PRIORITY_HIGH_ACCURACY. If I try to use LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY (its my default for s8) on all devices that I checked, it will start mode "wifi and other networks", but all time the same mode.

I use FusedLocationClient. My code to start system dialog:

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(getLocationRequest());
        builder.setAlwaysShow(true);
        SettingsClient client = LocationServices.getSettingsClient(this);
        Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                if (e instanceof ResolvableApiException) {
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        ResolvableApiException resolvable = (ResolvableApiException) e;
                        resolvable.startResolutionForResult(MapsActivity.this,
                                REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                        // Ignore the error.
                        if (sendEx.getMessage() != null) {
                            Crashlytics.logException(new RuntimeException("startGpsService: " + sendEx.getMessage()));
                        }
                    }
                }
            }
        });

And my LocationRequest:

if (mLocationRequest == null) {
            mLocationRequest = new LocationRequest();
            if (Build.MANUFACTURER.toLowerCase().contains("samsung") &&
                    Build.MODEL.toLowerCase().contains("sm-g95")) {
                mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            } else {
                mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            }
            mLocationRequest.setInterval(2000);
            mLocationRequest.setFastestInterval(1000);
        }
        return mLocationRequest;

I'll be glad to get any suggestions.


Update, I make some tests, this is the log:

2018-11-20 09:56:11.055 29850-29850 D/TAG_MAP_PROVIDER_CHECK: onActivityResult: NETWORK_PROVIDER && GPS_PROVIDER both enabled
2018-11-20 09:56:29.554 30111-30111 D/TAG_MAP_PROVIDER_CHECK: onActivityResult: location providers not enabled
2018-11-20 09:56:48.847 30309-30309 D/TAG_MAP_PROVIDER_CHECK: onActivityResult: NETWORK_PROVIDER && GPS_PROVIDER both enabled
2018-11-20 09:57:30.645 30647-30647 D/TAG_MAP_PROVIDER_CHECK: onActivityResult: location providers not enabled
2018-11-20 09:57:58.354 30908-30908 D/TAG_MAP_PROVIDER_CHECK: onActivityResult: NETWORK_PROVIDER && GPS_PROVIDER both enabled
2018-11-20 09:58:16.632 31159-31159 D/TAG_MAP_PROVIDER_CHECK: onActivityResult: location providers not enabled
2018-11-20 09:59:57.753 1665-1665 D/TAG_MAP_PROVIDER_CHECK: onActivityResult: NETWORK_PROVIDER && GPS_PROVIDER both enabled
2018-11-20 10:00:14.176 2200-2200 D/TAG_MAP_PROVIDER_CHECK: onActivityResult: NETWORK_PROVIDER && GPS_PROVIDER both enabled
2018-11-20 10:00:30.406 2963-2963 D/TAG_MAP_PROVIDER_CHECK: onActivityResult: NETWORK_PROVIDER && GPS_PROVIDER both enabled
2018-11-20 10:00:45.215 3398-3398 D/TAG_MAP_PROVIDER_CHECK: onActivityResult: location providers not enabled

All the time when I get "location providers not enabled" it comes to onActivityResult with (requestCode == REQUEST_CHECK_SETTINGS && resultCode == RESULT_CANCELED) and turn on "Device only" mode.

Its looks like it depends on place where exactly was my finger on "Yes" button. Additional question: If I can make custom view for system "turn on location" dialog ?