Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:我可以在不将用户重定向到“设置”屏幕的情况下启用GPS吗;谷歌地图;应用程序_Android_Android Settings_Android Gps - Fatal编程技术网

Android 安卓:我可以在不将用户重定向到“设置”屏幕的情况下启用GPS吗;谷歌地图;应用程序

Android 安卓:我可以在不将用户重定向到“设置”屏幕的情况下启用GPS吗;谷歌地图;应用程序,android,android-settings,android-gps,Android,Android Settings,Android Gps,在基于GPS的应用中,用户启用其GPS非常重要。如果没有,我们通常会显示一个对话框,说明用户“应通过设置启用其GPS,以便能够使用此功能” 当用户按OK时,他将被重定向到设置页面,我不喜欢这个解决方案,因为它会将用户从应用程序上下文中带到设置中 我注意到“谷歌地图”应用程序有一个更好的解决方案,那就是在需要GPS功能时显示一个整洁的对话框。用户选择“确定”后,GPS将直接启用,无需重定向到设置 我可以在不将用户重定向到“谷歌地图”应用程序中的设置屏幕的情况下启用GPS吗? 签出以下图像: 要拥有

在基于GPS的应用中,用户启用其GPS非常重要。如果没有,我们通常会显示一个对话框,说明用户“应通过设置启用其GPS,以便能够使用此功能”

当用户按OK时,他将被重定向到设置页面,我不喜欢这个解决方案,因为它会将用户从应用程序上下文中带到设置中

我注意到“谷歌地图”应用程序有一个更好的解决方案,那就是在需要GPS功能时显示一个整洁的对话框。用户选择“确定”后,GPS将直接启用,无需重定向到设置

我可以在不将用户重定向到“谷歌地图”应用程序中的设置屏幕的情况下启用GPS吗?

签出以下图像:


要拥有所需的功能:

  • 首先(至少)播放服务的7.0版
编译'com.google.android.gms:play services位置:16.0.0'

  • 代码中的第二个类似内容(我在onCreate中有):
-


我还没有确认答案,稍后再试。感谢您的帮助谷歌游戏服务是一个huuge库。要仅包含location api,请使用
compile'com.google.android.gms:play services location:8.1.0'
awesome。谢谢为我节省了很多时间。@AlvaroSantisteban能否提供一个到官方文档的链接,以获得您的答案?您是否想来对了@A.Alqadomi@AndreHoffmann在我们的新项目中,我的同事已经完成了。秘密在于使用谷歌的新API,该API基于从“谷歌游戏服务”获取位置。剩下的就直截了当了
 // 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);
// 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;
        }
    }
};
/**
 * 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;
    }
}