如何停止位置管理器?

35

我不知道为什么,但有时候即使关闭了应用程序,LocationManager仍然在工作。

我在一个活动的onCreate方法中调用startGPS()(只有一个活动,我称之为StartActivity)。

protected void startGPS(){    
 try {           
     lmanager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
     lmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
     lmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
 } catch(Exception e) {
     e.printStackTrace();
 }
}

如果这个活动将被销毁(也就是当应用程序关闭时),我会调用endGPS()函数。

public void endGPS(){
 try {           
     lmanager.removeUpdates(this);
     lmanager=null;
 } catch(Exception e) {
  e.printStackTrace();
 }
}

有一些想法和建议,我做错了什么?!


Schwiz的回答似乎是最有可能的解决方案。为了确认,请在endGPS()方法中添加一个调试日志条目,并通过在Eclipse中查看LogCat来查看它们是否在应用程序关闭时被调用。 - Brian
1
你是如何确定“LocationManager在关闭应用程序后仍在工作”的呢?此外,我很担心您会为同一个监听器对象调用requestLocationUpdates()两次。 - CommonsWare
1
@CommonsWare 我可以在状态栏中看到GPS图标。我应该使用两个不同的监听器吗?!我刚刚在这里阅读到“您还可以通过调用requestLocationUpdates()两次(一次为NETWORK_PROVIDER,一次为GPS_PROVIDER)从GPS和网络位置提供程序请求位置更新。”http://developer.android.com/guide/topics/location/obtaining-user-location.html - Tima
6个回答

33

你应该在onPause方法中调用removeUpdates方法:

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
    Log.i(TAG, "onPause, done");
}

removeUpdates在我的情况下出错了。请帮我。 - Ankit Mishra
@AnkitMishra 当你用try/catch包围时出现了什么错误? - Matheus Santz

13

你的活动是否有可能没有被销毁?例如:你按下了主页键。将你的GPS启动/停止移至 onStartonPause 中。


明天我会检查一下,但我相当确定那个活动会被销毁。可能CommonsWare是对的,我应该使用两个监听器。但是我没有找到任何例子。 - Tima

8

一旦加载,模拟器永远不会消除GPS图标。因此,在模拟器上,您不能使用GPS图标来测试GPS是否仍在运行。但是,在设备上,该图标应该会消失。

我应该使用两个不同的监听器吗?

我肯定会这样做。我不知道removeUpdates()是否会同时删除两个请求,甚至是否将两个请求注册到单个监听器中。


那就是问题,我正在设备上测试我的应用程序,但图标没有消失。我尝试使用两个监听器来解决。 - Tima
1
好的,知道了。我之前真的很困惑。 - Mister Smith

6
我使用:locationManager.removeUpdates(locationListener);它可以正常工作。
    @Override
protected void onPause() {
    super.onPause();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }


    locationManager.removeUpdates(locationListener);
}

2

我看到这篇帖子已经有一段时间了,但也许对其他人有所帮助。我使用的是removeUpdates(this),因为我的监听器是在实现位置管理器的活动中,你需要指示你的监听器。


0

这是一个相当老的问题,但对我来说有效。

       @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        mLocationManager.removeUpdates(locationListener);

        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接