Android:检测用户打开状态栏
我正在编写一个使用 GPS 进行定位服务的应用程序。当用户尝试在禁用 GPS 的情况下使用该应用程序时,会弹出一个对话框。现在,用户可以最小化应用程序并更改 GPS 设置。当他重新打开应用程序时,它会在 onResume() 中检测到设置已更改并关闭对话框。 这工作正常,但用户还有第二种启用 GPS 的方法:状态栏。当状态栏打开和关闭时, onResume() 和 onWindowFocusChanged() 都不会被调用,因此我不会检测用户何时启用 GPS。
有没有办法检测状态栏的打开?
我可以通过 LocationListener 来做到这一点,但我想按照我上面写的方式来做到这一点。
提前致谢!
我在 android-dev IRC 找到了帮助。现在,我使用观察者来注意设置更改,例如此处。因此,我会检测所有设置更改,即使它们是通过通知栏完成的,而不需要从 LocationManager 请求更新。 感谢您的帮助!
I am writing an app that uses GPS for localization services. When the user tries to use the app with a disabled GPS, a dialog pops up. Now, the user can minimize the app and change the GPS settings. When he reopens the app, it detects in onResume() that the settings were changed and closes the dialog.
That works fine, but the user has a second way to enable GPS: the status bar. When the status bar is opened and closed, neither onResume() nor onWindowFocusChanged() gets called, so that I don't detect when the user enabled GPS.
Is there a way to detect the opening of the status bar?
I could do it via the LocationListener, but I would like to do it how I wrote above.
Thanks in advance!
I found help at android-dev IRC. Now, I am using an Observer to notice setting changes like here. So, I detect all setting changes, even if they are done via the notifications bar, without requesting updates from the LocationManager.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要监听用户已启用的 GPS,使用状态栏,您可以通过位置管理器设置监听器以进行更改:
在 onCreate() 内设置此监听器
,其中 gpsStatusListener 是以下实例:
For listening to GPS that user has enabled, using a status bar, you can setup a listener for changes via the location manager:
set this listener inside onCreate()
where gpsStatusListener is instance of:
您正在寻找的是 LocationManager.addGpsStatusListener 吗?
Is it LocationManager.addGpsStatusListener you are looking for?
这并不能直接解决您的问题,但您可能会发现为用户打开设置更容易:
This doesn't directly address your issue, but you may find it easier to open the settings for the user: