Kis*_*ama 4 android location android-10.0
在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.
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
得到结果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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Run Code Online (Sandbox Code Playgroud)
这是一个错误,这是谷歌问题跟踪器上的链接https://issuetracker.google.com/issues/140447198
同时,您可以仔细检查 GPS 状态onActivityResult以解决 Android 10 中的此问题