A.A*_*omi 15 android android-settings android-gps
在基于GPS的应用中,重要的是用户启用他的GPS.如果没有,那么通常我们会显示一个对话框,说明用户" 应该从设置启用他的GPS以便能够使用此功能 ".
当用户按下OK时,他将被重定向到Settings页面,我不喜欢这个解决方案,因为它将用户从应用程序上下文中带到了设置中.
我注意到"谷歌地图"应用程序有一个更好的解决方案,即在需要GPS功能时显示一个整洁的对话框.一旦用户选择"OK" GPS将被启用直接没有任何转向的设置.
我可以在不将用户重定向到设置屏幕的情况下启用GPS,就像在"谷歌地图"应用程序中一样吗?
看看下面的图片:

Alv*_*ban 21
要获得该功能,您需要:
compile 'com.google.android.gms:play-services-location:16.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);
Run Code Online (Sandbox Code Playgroud)
然后创建回调:
// 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;
}
}
};
Run Code Online (Sandbox Code Playgroud)
然后,最后,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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9685 次 |
| 最近记录: |