当应用程序空闲时如何停止 Android 中的 GPS 更新?
朋友们,
我创建了一个应用程序,在其中我获取 GPS 位置 onCreate 方法和
onPause 方法,我删除位置更新。
在进行 GPS 更新后,我让我的手机闲置,整个晚上它都处于闲置模式,我的应用程序正在其中运行,我猜早上的电池因 GPS 而关闭了。
有人指导我如何处理这种情况,如果应用程序除了 onPause() 之外处于空闲状态,我应该用哪种方法停止使用 GPS?
任何帮助将不胜感激。
friends,
i have created an application in which i am getting GPS location onCreate Method and
onPause method i remove Location updates.
after taking gps update i left my mobile idle and whole night it was in idle mode and my application was running in it, in the morning bettery was down due to GPS i guess.
any one guide me how to handle this situation in which method should i stop using GPS if application is idle other than onPause()?
any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
onPause()
中停止位置更新通知是可以的,但请注意两件事。onCreate()
中并在onPause()
中停止它,那么当您将应用程序返回到前台时,它将不会再次注册更新,请考虑将注册移至onResume()。
Stop the location update notification in
onPause()
is fine, but please note two things.onCreate()
and stop it inonPause()
, then when you return your application to foreground it will not register to updates again, consider moving the registration toonResume()
.我问了一个类似的问题,如果用户移动到找不到信号的室内,GPS 就会超时。我在 LocationListener 类中启动了一个计时器,如果在 5-8 秒内未找到信号,则删除更新。这里有一些代码可能会有所帮助如何使 GPS 信号采集超时
I asked a similar question about timing out the GPS if the user moves indoors where a signal cannot be found. I started a timer within the LocationListener class and remove updates if a signal isn't found within 5-8 seconds. There's some code here that might help How to time out GPS signal acquisition
如果您停止接收广播(从而在 Activity 的 onPause() 方法中取消注册 BroadcastReceiver),您的应用程序就没有理由继续从 LocationManager 接收更新。
If you stop receiving broadcasts (thus unregistering your BroadcastReceiver in the onPause() method of your activity), there is no reason why your application would keep receiving updates from the LocationManager.