在 Android 10 中,当以编程方式打开位置时,我得到结果代码 RESULT_CANCELED

In Android 10 when location is turned on programmatically, I get result code RESULT_CANCELED

Android 10 中,当以编程方式访问位置时,会显示一个打开位置的弹出窗口,当按下确定(即打开位置)时,然后onActivityResult 中的回调,其中我得到的结果代码是 Activity.RESULT_CANCELED 而不是 Activity.RESULT_OK。 phone 中的位置也已打开。

这在下面工作正常 Android 10. 仅在 android 中不起作用 10. 我得到结果代码这个 -Activity.RESULT_CANCELED

启用位置代码

activity.setFinishOnTouchOutside(true);

    final int REQUEST_LOCATION = 199;

    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Log.e("location", "Connect");
                }

                @Override
                public void onConnectionSuspended(int i) {
                    Log.e("location", "fail");
                    //googleApiClient.connect();
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult connectionResult) {
                    Log.d("location", "Location error " + connectionResult.getErrorCode());
                }
            }).build();
    googleApiClient.connect();

    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);

    SettingsClient client = LocationServices.getSettingsClient(activity);
    Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
    task.addOnSuccessListener(activity, new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            // All location settings are satisfied. The client can initialize
            // location requests here.
            // ...
            Log.d("location_enable", "enable");
        }
    });

    task.addOnFailureListener(activity, 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(activity,
                            REQUEST_LOCATION);
                } catch (IntentSender.SendIntentException sendEx) {
                    // Ignore the error.
                }
            }
        }
    });

得到结果onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_LOCATION) {
        switch (resultCode) {
            case Activity.RESULT_OK:
                //Success Perform Task Here
                getLocation();
                break;
            case Activity.RESULT_CANCELED:
                progressDialog.dismiss();
                method.alertBox(getResources().getString(R.string.please_allow_location));
                break;
        }
    }
}

AndroidManifest.xml

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

这是一个错误,这是 google 问题跟踪器 https://issuetracker.google.com/issues/140447198

上的 link

同时,您可以在 onActivityResult 中仔细检查 gps 状态,以在 android 10

中解决此问题