Activity.RESULT_CANCELED (value == 0 )
始终会在onActivityResult()
中被调用,即使在位置对话框上按“确定”
如果在设备设置中关闭了位置,我将向用户显示位置对话框。
使用了以下代码-
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY));
builder.setAlwaysShow(true);
mLocationSettingsRequest = builder.build();
mSettingsClient = LocationServices.getSettingsClient(WifiList_Activity.this);
mSettingsClient
.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
//Success Perform Task Here
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException rae = (ResolvableApiException) e;
rae. startResolutionForResult(WifiList_Activity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sie) {
Logger.log("GPS Unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Logger.log("GPS Location settings are inadequate, and cannot be fixed here. Fix in Settings.");
}
}
});
onActivityResult()
中的代码如下,
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CHECK_SETTINGS) {
switch (resultCode) {
case Activity.RESULT_CANCELED:
Logger.log("Result code in onACtivityresult CANCELED::"+resultCode);
Logger.log("GPS User denied to access location");
finish();
break;
case Activity.RESULT_OK:
//Success Perform Task Here
Logger.log("Result code in onACtivityresult OK::"+resultCode);
break;
}
}
但是问题是,即使用户在“位置”对话框上按“确定”,由于接收到的resultCode为0,总是会调用Acivity.RESULT_CANCELED
。
我已将应用定位到Android Q
,
compileSDKVersion "android-Q'
targetSDKVersion 'Q'
此外,在Pixel设备上的Android Q beta上运行该应用。
预期-按下确定对话框时,resultCode -1应该为 返回onActivityResult。
实际-即使按ok,也将返回resultCode 0 onActivityResult。