编辑 Margaret Bloom's link正是我想要的。
我正在编写一个应用程序,根据您当时的位置推荐食物,所以没有位置信息我的应用程序基本上没用。以下是我以编程方式访问位置数据的方法。
public void onConnected(Bundle bundle) {
//adds the location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocReq);
//make sure the phone is giving us what we asked for
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS: //we have what we want
Log.v("Location settings", "Location settings are satisfied.");
startLocationUpdates();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.v("Location Settings", "Location settings not satisfied. Starting REQUEST_CHECK_SETTINGS");
//we need to ask user before phone will give location information
try {
status.startResolutionForResult(LaunchActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// This phone can't give us what we need.
Log.e("Location Settings", "The user \"never\" wants to give us location info");
try {
status.startResolutionForResult(LaunchActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
}
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHECK_SETTINGS: //if we got the request check settings callback
switch (resultCode) {
case Activity.RESULT_OK:
Log.i("Location Settings", "onActivityForResult returned RESUME_OK");
startLocationUpdates();
break;
case Activity.RESULT_CANCELED:
Log.i("Location Settings", "user did not want to give us location permissions");
break;
}
break;
}
}
如果用户在对话框中选择“从不”,则设置 LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:大小写并且手机不再允许我显示位置请求对话框。重新启动应用程序并手动启用位置数据也不起作用,我的应用程序无法发送位置更新。
因此,从编程方面和UX方面,我可以提示用户再次提供位置权限的最佳方法是什么?