gpt4 book ai didi

android - 如何在 getAccuracy() 低于 100 时停止处理程序

转载 作者:太空狗 更新时间:2023-10-29 16:28:46 27 4
gpt4 key购买 nike

您好,我正在创建一个可以获取用户位置的 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 中的 Location 并在该位置足够准确时执行某些操作。那么您根本不需要 Runnable。

关于android - 如何在 getAccuracy() 低于 100 时停止处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44143210/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com