在文档here中,我正在关注如何接收位置请求以及我注意到这两个代码块中的两个不寻常的事情:
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates = result.getLocationSettingsStates(); //<--This line I don't understand
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
...
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
OuterClass.this,
REQUEST_CHECK_SETTINGS);
} catch (SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
...
break;
}
}
});
在第二个街区:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent); //<--And this line
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
...
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
...
break;
default:
break;
}
break;
}
}
我添加了一些箭头指向我不理解的线条。特别是这两个:
final LocationSettingsStates = result.getLocationSettingsStates();
final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
第一行是我以前没见过的。这是如何有效的,为数据类型赋值?然后该类不再在该代码块中的任何其他地方使用,那么赋值的目的是什么?
在另一行中,现在它正在为该数据类型的states
实例分配一个值,但该实例不会在onActivityResult()
中的任何其他位置使用。
那么这里发生了什么?感谢。
答案 0 :(得分:0)
第一行肯定是拼写错误;它应该是这样的:
final LocationSettingsStates states = result.getLocationSettingsStates();
是的,它并没有在任何一个地方使用,这很奇怪,但是你可以在它上面调用things isBleUsable()
来确定现在究竟存在和可用的内容。在后一种情况下,它是在尝试解决之后可以使用的。