【问题标题】:Android: Can I enable the GPS without redirecting the user to the settings screen like in "google maps" appAndroid:我可以在不将用户重定向到“谷歌地图”应用程序中的设置屏幕的情况下启用 GPS
【发布时间】:2015-06-26 23:08:39
【问题描述】:

在基于 GPS 的应用程序中,用户启用他的 GPS 非常重要。如果不是,那么通常我们会显示一个对话框,说明用户“应该从设置中启用他的 GPS 才能使用此功能”。

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

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

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

查看下图:

【问题讨论】:

  • 你来对了吗? @A.Alqdomi
  • @AndreHoffmann 我的同事在我们的新项目中已经完成了。秘诀是使用来自 Google 的新 API,它基于从“google play services”获取位置。其余的很简单

标签: android android-settings android-gps


【解决方案1】:

要拥有您需要的功能:

  • 首先(至少)7.0 版的播放服务

compile 'com.google.android.gms:play-services-location:16.0.0'

  • 在您的代码中第二个类似这样的内容(我的 onCreate 中有它):

-

 // 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;
        }
    }
};

最后,在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;
    }
}

【讨论】:

  • 我还没有确认答案,稍后再试。感谢您的帮助
  • Google play services 是一个巨大的图书馆。要仅包含位置 api,请使用 compile 'com.google.android.gms:play-services-location:8.1.0'
  • 太棒了。谢谢。为我节省了很多时间。
  • @AlvaroSantisteban 能否提供官方文档的链接以供您回答?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-05
  • 2015-03-31
相关资源
最近更新 更多