您好,我正在创建一个 Android map 应用程序,它将获取用户位置,但是当用户打开 map 时,有时位置不准确,并且会慢慢精确定位您的位置。
这个我已经试过了。但它仍然调用处理程序并且 TOASTS 不会停止。
private LocationManager locationManager;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps_page);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
handler.postDelayed(runnable,3000);
}
@Override
public void onProviderDisabled(String provider) {
handler.removeCallbacks(runnable);
}
});
private Runnable runnable = new Runnable() {
@Override
public void run() {
getCurrentLocation();
Toast.makeText(mapsPage.this, "GETTING LOCATION", Toast.LENGTH_SHORT).show();
handler.postDelayed(this, 3000);
}
};
private void getCurrentLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location != null) {
longitude = location.getLongitude();
latitude = location.getLatitude();
moveMap();
Integer loc = Math.round(location.getAccuracy());
textings.setText(Integer.toString(loc));
if(loc <= 100)
{
handler.removeCallbacks(runnable);
Toast.makeText(mapsPage.this, "HANDLER STOPPED", Toast.LENGTH_SHORT).show();
}
}
}
//Function to move the map
private void moveMap() {
LatLng latLng = new LatLng(latitude, longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(17));
mMap.addMarker(new MarkerOptions().position(latLng).draggable(false));
}
我添加了一个处理程序,该处理程序将每 5 秒运行一次,检查用户位置,当 getAccuracy 数据等于或小于 100 时,它将停止。我该怎么做?
最佳答案
getCurrentLocation 可能会调用 handler.removeCallbacks(runnable);
,但 runnable 将始终在之后调用 handler.postDelayed(this, 3000);
。
要解决此问题,Runnable 必须满足某些条件才能检查它是否应该再次发布自身。
解决方案是让 getCurrentLocation 返回一个 bool 值,指示它是否成功(足够):
private boolean getCurrentLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return false;
}
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location != null) {
longitude = location.getLongitude();
latitude = location.getLatitude();
moveMap();
Integer loc = Math.round(location.getAccuracy());
textings.setText(Integer.toString(loc));
if(loc <= 100) {
handler.removeCallbacks(runnable);
Toast.makeText(mapsPage.this, "HANDLER STOPPED", Toast.LENGTH_SHORT).show();
return true;
}
}
return false;
}
然后在您的 Runnable 中检查是否需要再次运行:
@Override
public void run() {
if(!getCurrentLocation()) {
handler.postDelayed(this, 3000);
}
}
但是,话虽如此,您应该只检查 LocationListener 的 onLocationChanged 中的位置,并在该位置足够准确时执行某些操作。那么你根本不需要 Runnable。
关于android - 当 getAccuracy() 低于 100 时如何停止处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44143210/