取消订阅LocationManager中的LocationListener

Gre*_*g B 5 android locationmanager locationlistener

如何取消订阅LocationListener收到的更新LocationManager

这是我如何设置它

mLocationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
mListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        Log.i("LocationListener", "Logging Change");
    }

}

mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                5000, 1, mListener);
Run Code Online (Sandbox Code Playgroud)

在我退出创建的视图后,LocationListener我仍在LogCat窗口中获取日志消息.

我明白这是因为我正在孤立侦听器,但我看不到任何破坏方法,LocationListener也不能在LocationManager对象上看到任何"删除侦听器"样式方法.

Mar*_*k B 14

在LocationManager上调用removeUpdates,传递您的位置监听器.

  • 如果您不将侦听器存储为成员,是否有任何方法可以执行此操作? (2认同)

Chi*_*han 6

mLocationManager.removeUpdates(mListener);
Run Code Online (Sandbox Code Playgroud)