如何停止位置管理器?
不知道为什么,但有时 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我发现这篇文章已经有一段时间了,但也许它会对其他人有所帮助。我使用: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.
相当老的怪人,但这对我有用。
Quite an old queerie but this works for me.
您应该在方法
onPause
内调用方法removeUpdates
:You should call the method
removeUpdates
inside the methodonPause
:您的活动有可能没有被破坏吗?即:您按下了主页按钮。将 GPS 开始/停止移动到
onStart
和onPause
。Is it possible your activity isn't being destroyed? i.e.: you hit the home button. Move your gps start/stop to
onStart
andonPause
.一旦加载,模拟器就永远不会摆脱 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.
I sure would. I do not know whether
removeUpdates()
will remove both, or even if both requests are registered with the single listener.我使用:locationManager.removeUpdates(locationListener);它的工作原理
I use: locationManager.removeUpdates(locationListener); Its Working