【问题标题】:Unsubscribing a LocationListener from the LocationManager从 LocationManager 取消订阅 LocationListener
【发布时间】:2010-01-19 22:01:55
【问题描述】:

我如何取消订阅 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);

退出创建 LocationListener 的视图后,我仍然在 LogCat 窗口中收到日志消息。

我知道这是因为我正在孤立侦听器,但我看不到 LocationListener 上的任何销毁方法,也看不到 LocationManager 对象上的任何“删除侦听器”样式方法。

【问题讨论】:

    标签: android locationmanager locationlistener


    【解决方案1】:

    在 LocationManager 上调用 removeUpdates,传递您的位置侦听器。

    【讨论】:

    • 如果您不将侦听器存储为成员,有什么办法吗?
    【解决方案2】:
    mLocationManager.removeUpdates(mListener);
    

    【讨论】:

      【解决方案3】:

      我认为removeUpdates 应该会有所帮助。

      mLocationManager.removeUpdates(mListener)
      

      【讨论】:

      • 如果您不将侦听器存储为成员,有什么办法吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2020-08-29
      • 2016-07-23
      • 2022-01-19
      • 2017-12-18
      相关资源
      最近更新 更多