如何停止位置管理器?

发布于 2024-10-02 23:55:08 字数 748 浏览 1 评论 0原文

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

我在一个 Activity 的 onCreate-Methode 中调用 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();
 }
}

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

Don't know why, but sometimes LocationManager is still working also after closing application.

I call startGPS() in onCreate-Methode in one Activity (only one, let me call it 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();
 }
}

And if this activity will be destroyed (so, when application will be closed), I call endGPS()

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

Some ideas, some suggestions, what have I done wrong?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

临走之时 2024-10-09 23:55:09

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

I see it´s been a while since this post, but maybe it would help some body else. I use: removeUpdates(this), because my listener it´s the activity where I implement de location manager, you need to indicate your listener.

萌化 2024-10-09 23:55:09

相当老的怪人,但这对我有用。

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

        mLocationManager.removeUpdates(locationListener);

        }

Quite an old queerie but this works for me.

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

        mLocationManager.removeUpdates(locationListener);

        }
你对谁都笑 2024-10-09 23:55:08

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

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

You should call the method removeUpdates inside the method onPause:

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
    Log.i(TAG, "onPause, done");
}
弥枳 2024-10-09 23:55:08

您的活动有可能没有被破坏吗?即:您按下了主页按钮。将 GPS 开始/停止移动到 onStartonPause

Is it possible your activity isn't being destroyed? i.e.: you hit the home button. Move your gps start/stop to onStart and onPause.

热鲨 2024-10-09 23:55:08

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

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

我肯定会的。我不知道 removeUpdates() 是否会删除这两个请求,或者即使两个请求都注册到单个侦听器中。

The emulator never gets rid of the GPS icon once loaded. Hence, on an emulator, you cannot use the GPS icon as a test as to whether GPS is still running. On a device, though, the icon should vanish.

Should I use two different listeners?

I sure would. I do not know whether removeUpdates() will remove both, or even if both requests are registered with the single listener.

墟烟 2024-10-09 23:55:08

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

I use: locationManager.removeUpdates(locationListener); Its Working

    @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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文