Android LocationManager.removeUpdates(LocationListener)



嗨,伙计们,我已经创建了一个后台服务,它获得人的位置,如果设备到目的地之间的距离较小,我请求位置更新更频繁,我使用removeUpdates(LocationListener)方法…问题是,我注意到监听器不断得到更新,循环越多,它就会得到越来越多的更新。有人知道为什么这个方法不起作用吗?

以下是我使用新位置的方法。
    void makeUseOfNewLocation(Location location){
    manager.removeUpdates(listener);
    mLocation = location;
    currentLat = location.getLatitude();
    currentLong = location.getLongitude();
    Location.distanceBetween(currentLat, currentLong, gateLat, gateLong, results);
    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    currentDistanceFromDestination = results[0];
    Log.d("Current Location", "Lat:"+currentLat+"   Long:"+currentLong);
    Log.d("Destination Distance", currentDistanceFromDestination+"");
    if(currentDistanceFromDestination<3000){
        if(currentDistanceFromDestination<50){
            Log.d("50m closer", "Calling");
            startActivity(callIntent);
            stopSelf();
        }
        if(currentDistanceFromDestination<800){
            Log.d("800m closer", "Started listening every 10 seconds.");
            Toast.makeText(getApplicationContext(),currentDistanceFromDestination+"", Toast.LENGTH_LONG).show();
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 ,90 , listener);
        }else{
            Log.d("3000m closer", "Started listening every 25 seconds");
            Toast.makeText(getApplicationContext(),currentDistanceFromDestination+"", Toast.LENGTH_LONG).show();
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 25 * 1000, 300, listener);
        }
    }
}

是否在呼叫之间初始化侦听器的值?它可能不是同一个对象,因此不能删除对它的更新。在这种情况下,旧的侦听器仍将获得更新,而您无法删除它们。

最新更新