【发布时间】:2018-02-23 10:52:38
【问题描述】:
这是我提示用户启用 GPS 定位的示例代码。
private void showLocationSettingsRequest(Context context) {
try {
/* Initiate Google API Client */
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(SPPlaysetScanActivity.this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000 / 2);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.d(TAG, "showLocationSettingsRequest :PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Log.d(TAG, "showLocationSettingsRequest :Location settings are inadequate,
// and cannot be fixed here. Dialog not created.");
break;
}
}
});
} catch (Exception e) {
Log.d(TAG, "showLocationSettingsRequest EX:" + e.toString());
}
}
如果我们从状态栏设置中禁用位置,我们能够获得位置设置对话框,但是当我们打开位置(从顶部状态栏)时,仍然会出现位置对话框。从状态栏设置打开位置时,我需要关闭对话框。
提前致谢!
【问题讨论】:
标签: android gps location android-statusbar