Android 如何停止位置管理器?

Android 如何停止位置管理器?,android,gps,locationmanager,Android,Gps,Locationmanager,不知道为什么,但有时LocationManager在关闭应用程序后仍在工作 我在一个活动中调用onCreate Methode中的startGPS() 若这个活动将被销毁(所以,当应用程序关闭时),我调用endGPS() 一些想法,一些建议,我做错了什么 您的活动是否可能没有被破坏?i、 你按了主按钮。将gps开始/停止移动到onStart和onPause模拟器在加载后永远不会摆脱gps图标。因此,在模拟器上,不能使用GPS图标测试GPS是否仍在运行。不过,在设备上,图标应该消失 我应该使用两个

不知道为什么,但有时LocationManager在关闭应用程序后仍在工作

我在一个活动中调用onCreate Methode中的startGPS()

若这个活动将被销毁(所以,当应用程序关闭时),我调用endGPS()


一些想法,一些建议,我做错了什么

您的活动是否可能没有被破坏?i、 你按了主按钮。将gps开始/停止移动到
onStart
onPause

模拟器在加载后永远不会摆脱gps图标。因此,在模拟器上,不能使用GPS图标测试GPS是否仍在运行。不过,在设备上,图标应该消失

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


我当然会的。我不知道
removeUpdates()
是否会同时删除这两个请求,或者即使这两个请求都是在一个侦听器中注册的。

我知道这篇文章发布已经有一段时间了,但它可能会对其他人有所帮助。我使用:removeUpdates(this),因为我的侦听器这是我实现位置管理器的活动,所以您需要指出您的侦听器。

您应该在方法
onPause
内调用方法
removeUpdates

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

我使用: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);
}

很老了,但这对我有用

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

        mLocationManager.removeUpdates(locationListener);

        }

Schwiz的答案似乎是最有可能的解决方案。要确认,请将调试日志项添加到endGPS()方法中,并通过查看Eclipse中的LogCat来查看是否在关闭应用程序时调用它们。如何确定“关闭应用程序后LocationManager仍在工作”?另外,我还担心您对同一侦听器对象调用两次
requestLocationUpdates()
。@commonware我可以在状态栏中看到gps图标。我应该用两个不同的听众吗?!我刚刚读到“您也可以通过调用requestLocationUpdates()两次从GPS和网络位置提供商请求位置更新,一次用于网络位置提供商,一次用于GPS位置提供商。”在这里,我明天会检查它,但我确信,该活动将被销毁。也许Commonware是对的,我应该使用两个侦听器。但我还没有发现任何问题的例子,我正在设备上测试我的应用程序,图标并没有消失。我试着和两位听众一起工作,这很好理解。我对此感到非常困惑。removeUpdates在我的案例中给出了错误。请帮帮我
    @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);
}
       @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        mLocationManager.removeUpdates(locationListener);

        }