android - 当我们从顶部状态栏启用位置时,如何关闭 LocationSettingsRequest 对话框?

标签 android gps location android-statusbar

This is my sample code for prompting the user for to enable GPS location.

 private void showLocationSettingsRequest(Context context) {
    try {
        /* Initiate Google API Client  */
        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(SPPlaysetScanActivity.this)
                .addApi(LocationServices.API)
                .build();
        googleApiClient.connect();

        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(10000);
        locationRequest.setFastestInterval(10000 / 2);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
        builder.setAlwaysShow(true);

        PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            // Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
                            status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            Log.d(TAG, "showLocationSettingsRequest :PendingIntent unable to execute request.");
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Log.d(TAG, "showLocationSettingsRequest :Location settings are inadequate,
                        // and cannot be fixed here. Dialog not created.");
                        break;
                }
            }
        });

    } catch (Exception e) {
        Log.d(TAG, "showLocationSettingsRequest EX:" + e.toString());
    }
}

If we disable location from status bar setting we are able to get the location settings dialog but when we turn on location (from the top status bar) but still the location dialog appears. I need to dismiss dialog when the location is turned on from status bar settings.

提前致谢!

最佳答案

这在您的代码方面是不可能的。 调用:

status.startResolutionForResult(SPActivity.this, REQUEST_CHECK_SETTINGS);

您正在打开单独的Activity(来自 Google Play 服务库)。此代码应该足够智能,注册 BroadcastReceiver 进行位置更改,然后在启用 GPS 的情况下将结果返回到您的 Activity

由于您无法更改此代码,我想您需要做的是在 Google 问题跟踪器网站上编写更改请求:https://source.android.com/setup/report-bugs

关于android - 当我们从顶部状态栏启用位置时,如何关闭 LocationSettingsRequest 对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48946146/

相关文章:

java - Android中获取当前位置方向的位置

android - 测量 tensorflow-lite android 版本的预测精度

Android 在来电时关闭媒体服务

javascript - 如何从 Android 浏览器中打开的网页中检测用户的位置?

mysql - 使用 Arduino 和 GPS/GPRS 模块的 GPS 数据不准确

ios - 如何在没有互联网连接的情况下从位置获取地址

java - 我想更改名为 .hannesdorfmann :swipeback 的库中的一些内容

android - SL4A 与 Ruboto 在 Android 应用程序开发上的对比

java - Android GPS 起初工作,但后来返回空值

swift - iOS swift 3 : Location Privacy Denied