LocationSettingRequest 无法将 GPS 更改为 PRIORITY_HIGH_ACCURACY
LocationSettingRequest fails to change GPS to PRIORITY_HIGH_ACCURACY
当位置设置关闭时,我的应用程序可以很好地打开位置并将优先级设置为高,但是当位置设置为 GPS LocationRequest.PRIORITY_LOW_POWER
而不是显示对话框以更改为 [=14] 时它会崩溃=]
这里是访问SettingClient的方法
public void locationSettingRequest() {
LocationRequest mLocationRequestHighAccuracy = new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationRequest mLocationRequestBalancedPowerAccuracy = new LocationRequest().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequestHighAccuracy)
.addLocationRequest(mLocationRequestBalancedPowerAccuracy);
Task<LocationSettingsResponse> result =
LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
result.addOnCompleteListener(task -> {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
getLoc();
} catch (ApiException exception) {
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
try {
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult(
SignUpActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
} catch (ClassCastException e) {
// Ignore, should be an impossible 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;
}
}
});
}
并且我在 OnActivityResult
中收到了响应
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
if (requestCode == REQUEST_CHECK_SETTINGS) {
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
getLoc();
break;
case Activity.RESULT_CANCELED:
finish();
break;
default:
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
如何让位置从 LocationRequest.PRIORITY_LOW_POWER
更改为 HIGH_ACCURACY
?
我就是这样解决的,我检查了LocationManager.NETWORK_PROVIDER
是否启用。如果不是,我会显示一条消息,提示用户将位置模式更改为 HIGH_ACCURACY
。如果启用 NETWORK_PROVIDER
,我会调用 settingRequest
。
不确定这是否正确,但它确实有效。
public void checkLocationMode() {
LocationManager loc = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (!loc.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Toast.makeText(this, "Turn location mode to High Accuracy.", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
} else {
settingRequest();
}
}
当位置设置关闭时,我的应用程序可以很好地打开位置并将优先级设置为高,但是当位置设置为 GPS LocationRequest.PRIORITY_LOW_POWER
而不是显示对话框以更改为 [=14] 时它会崩溃=]
这里是访问SettingClient的方法
public void locationSettingRequest() {
LocationRequest mLocationRequestHighAccuracy = new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationRequest mLocationRequestBalancedPowerAccuracy = new LocationRequest().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequestHighAccuracy)
.addLocationRequest(mLocationRequestBalancedPowerAccuracy);
Task<LocationSettingsResponse> result =
LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
result.addOnCompleteListener(task -> {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
getLoc();
} catch (ApiException exception) {
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
try {
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult(
SignUpActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
} catch (ClassCastException e) {
// Ignore, should be an impossible 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;
}
}
});
}
并且我在 OnActivityResult
中收到了响应 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
if (requestCode == REQUEST_CHECK_SETTINGS) {
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
getLoc();
break;
case Activity.RESULT_CANCELED:
finish();
break;
default:
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
如何让位置从 LocationRequest.PRIORITY_LOW_POWER
更改为 HIGH_ACCURACY
?
我就是这样解决的,我检查了LocationManager.NETWORK_PROVIDER
是否启用。如果不是,我会显示一条消息,提示用户将位置模式更改为 HIGH_ACCURACY
。如果启用 NETWORK_PROVIDER
,我会调用 settingRequest
。
不确定这是否正确,但它确实有效。
public void checkLocationMode() {
LocationManager loc = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (!loc.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Toast.makeText(this, "Turn location mode to High Accuracy.", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
} else {
settingRequest();
}
}