在文档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()
中的其他任何地方都没有使用。那么这是怎么回事?谢谢。
最佳答案
第一行肯定是拼写错误;它应该是这样的:
final LocationSettingsStates states = result.getLocationSettingsStates();
是的,这两个地方都没有使用它,这很奇怪,但是您可以像
isBleUsable()
一样调用things来确定确切存在的内容和当前可用的内容。在后一种情况下,这是尝试解决后可用的功能。关于java - 接收位置请求时,LocationSettingsStates类做什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37242018/