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

标签 android google-maps google-maps-android-api-2 handler

您好,我正在创建一个 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/

相关文章:

javascript - (Javascript) 将 Codepen 中的变量从 JS 窗口传递到 HTML &lt;script&gt;&lt;/script&gt; 模板

android - 将 Google Direction API 与多个航路点和模式结合使用

android - com.google.android.gms 在哪里?

android - onTokenRefresh 未在已签名的 APK 中调用,如果安装的先前版本的应用程序未实现 FCM

android - 多个设备/模拟器 - 尝试使用 Genymobile 进行转换时出错

android - 应用未在适用于平板设备的 Google Play 商店中列出

java - 编译 ionic 项目有问题吗?

java - Google map 未显示在 Android 设备上

android - GoogleMap.OnCameraChangeListener onCameraChange 与延迟一起工作

android - 在 Android 上调整谷歌地图 v2 的大小