在基于GPS应用,重要的是用户使他的全球定位系统是很重要的。如果没有,那么我们通常会显示一个对话框,说明用户的应该使自己的全球定位系统的设置,可以使用此功能的。
In GPS based applications, it is important that the user enable his GPS. If not then usually we would show a dialog stating that the user "should enable his GPS from the settings to be able to use this functionality".
当用户preSS确定,他将被重定向到设置页面,我不喜欢这样的解决方案,因为它需要用户从应用程序上下文中的设置。
When the user press OK he will be redirected to the Settings page, I don't like this solution since it takes the user out of the application context in to the settings.
我已经注意到,谷歌地图应用程序有一个更好的解决方案,这是显示一个整洁的对话框时,需要一个GPS功能。当用户选择确定GPS将被启用的直接没有任何转向的设置。
I have noticed that "google maps" application has a better solution, which is to show a neat dialog when a GPS feature is needed. Upon the user's selection "OK" GPS will be enabled directly without any redirection to the settings.
我可以启用GPS无需将用户重定向到设置屏幕像谷歌地图应用程序?
下面结账图片:
要拥有你需要的功能:
在第一个(至少)的播放服务7.0版本 编译com.google.android.gms:玩-服务:7.0.0
-
// Check the location settings of the user and create the callback to react to the different possibilities
LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, locationSettingsRequestBuilder.build());
result.setResultCallback(mResultCallbackFromSettings);
然后创建回调:
And then create the callback:
// The callback for the management of the user settings regarding location
private ResultCallback<LocationSettingsResult> mResultCallbackFromSettings = new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
//final LocationSettingsStates locationSettingsStates = result.getLocationSettingsStates();
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(
MapActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.e(TAG, "Settings change unavailable. We have no way to fix the settings so we won't show the dialog.");
break;
}
}
};
然后,最后,在 onActivityResult
我有以下内容:
/**
* Used to check the result of the check of the user location settings
*
* @param requestCode code of the request made
* @param resultCode code of the result of that request
* @param intent intent with further information
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
//final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
if (mGoogleApiClient.isConnected() && userMarker == null) {
startLocationUpdates();
}
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
break;
default:
break;
}
break;
}
}