LocationSettings请求对话框再次出现

问题描述 投票:1回答:2

我正在使用Google Play服务。对于我的应用程序,位置是关键组成部分。当GPS关闭时,我使用LocationSettingsClient向用户显示一个对话框以启用它。问题是,按OK后,某些设备上再次出现对话框。

enter image description here

这是我的显示位置设置的代码:

onResume

override fun onResume() {
        super.onResume()
        Log.v(TAG, "onResume")
        this.startLocationUpdates(UPDATE_INTERVAL_IN_MILLISECONDS, UPDATE_DISTANCE_THRESHOLD)
    }

private fun startLocationUpdates(interval: Long, distance: Float = 0f) {
        if (!mSettingsClientInit)
            return

        createLocationRequest(interval, distance)
        createLocationCallback()
        // Begin by checking if the device has the necessary location settings.
        mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
                .addOnSuccessListener(this, object : OnSuccessListener<LocationSettingsResponse> {

                    override fun onSuccess(locationSettingsResponse: LocationSettingsResponse) {
                        Log.i(TAG, "LocationManager: All location settings are satisfied.");
                        mLocationCallback?.let {
                            fusedLocationClient?.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
                        }
//                        updateUI();
                    }
                })
                .addOnFailureListener(this, object : OnFailureListener {
                    override fun onFailure(e: Exception) {
                        Log.i(TAG, "Request PErmission failure");
                        var statusCode = (e as ApiException).getStatusCode()
                        when (statusCode) {
                            LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
                                try {
                                    // Show the dialog by calling startResolutionForResult(), and check the
                                    // result in onActivityResult().
                                    var rae = e as ResolvableApiException;
                                    rae.startResolutionForResult(this@BaseLocationActivity, REQUEST_CHECK_SETTINGS);
                                } catch (sie: IntentSender.SendIntentException) {
                                    Log.i(TAG, "PendingIntent unable to execute request.");
                                }
                            }
                            LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                                mRequestingLocationUpdates = false;
                            }

                        }
                    }
                }
                )
    }

[谁能指出出现LocationSettings对话框的原因一次又一次在某些设备上,不是在所有设备上?

android kotlin location google-play-services
2个回答
1
投票
之后

您将代码放入onReSume,因此,当您按OK时,此代码将再次运行并再次出现该对话框。您应该输入onCreate,此代码仅在运行应用程序时运行一次。


0
投票

将此代码放在您的onCreate中,而不是onResume中。因为当对话框关闭时,将调用活动的onResume。因此,您的对话框再次出现。

© www.soinside.com 2019 - 2024. All rights reserved.