我正在使用Google Play服务。对于我的应用程序,位置是关键组成部分。当GPS
关闭时,我使用LocationSettingsClient
向用户显示一个对话框以启用它。问题是,按OK
后,某些设备上再次出现对话框。
这是我的显示位置设置的代码:
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
对话框的原因一次又一次在某些设备上,不是在所有设备上?
您将代码放入onReSume,因此,当您按OK时,此代码将再次运行并再次出现该对话框。您应该输入onCreate,此代码仅在运行应用程序时运行一次。
将此代码放在您的onCreate中,而不是onResume中。因为当对话框关闭时,将调用活动的onResume。因此,您的对话框再次出现。