我有一个函数,它调用融合位置提供程序以获取位置信息并相应地更新UI:
private void getLocation() {
if (locationEnabled) {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(2000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
//update UI with relevant information, then stop location updates by calling:
stopLocationUpdates();
}
};
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// location permission not granted, fix it!
} else {
// location permission granted, start location updates
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
null /* Looper */);
}
}
}
这是stopLocationUpdates()
的样子:
private void stopLocationUpdates() {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
这是问题所在:通过单击按钮来调用功能getLocation()
。当我单击一次此按钮时,它似乎工作正常,并且一次使用位置信息更新了UI。但是当我快速单击按钮多次时,它“触发”了UI的不间断位置更新。