Android:我可以在不将用户重定向到 "google maps"应用程序中的设置屏幕的情况下启用 GPS

标签 android android-settings android-gps

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

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

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

我可以启用 GPS 而无需像“谷歌地图”应用那样将用户重定向到设置屏幕吗?

查看下图:

Neat Dialog

最佳答案

要拥有该功能,您需要:

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

编译 '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;
    }
}

关于Android:我可以在不将用户重定向到 "google maps"应用程序中的设置屏幕的情况下启用 GPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29744257/

相关文章:

java - 创建android子项目时出错

android - 打开 Android 的系统更新页面

java - 为 REST API 客户端证明 GPS 坐标的原点

java - 如何提高 AsyncTask 的速度?

android - 具有不同外观功能的相同应用程序,具体取决于下载位置?

android - 这是我第一次使用 android studio 并出现错误?不知道好像哪里出了问题?

android - 如何以编程方式打开 Android Q 中的设置面板?

java - 寻找某种模式

android - 限制 Android 中的应用程序缓存大小