Android LocationManager.removeUpdates(LocationListener)

时间:2014-07-07 03:19:16

标签: android gps listener locationmanager

大家好我创建了一个后台服务来获取人员位置,如果设备到目的地之间的距离较小,我会更频繁地请求位置更新,我使用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);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您是否在通话之间启动了听众的价值? 它可能不是同一个对象,因此无法删除它的更新。 在这种情况下,旧的监听器仍然会在您无法删除它们的情况下获得更新。