В Android 10 , при программном доступе к местоположению отображается всплывающее окно для включения местоположения, и при нажатии кнопки «ОК» (т. Е. Включить местоположение) выполняется обратный вызов в onActivityResult
, где я получаю код результатаActivity.RESULT_CANCELED
вместо Activity.RESULT_OK
.Кроме того, местоположение в телефоне включено.
Это прекрасно работает ниже 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"/>