我的 Android 应用程序不在屏幕上时应暂停活动
我正在编写的一个 Android 应用程序似乎会消耗大量电池,即使它不是前台应用程序。我的假设是,当应用程序不在前台时,我需要取消注册 GPS、方向等侦听器。
问题是,我有多种不同的活动可以在它们之间切换,并且只要我在它们之间切换,我就不希望听众被取消注册。但是,如果用户按住 Home 键并切换到另一个应用程序(或者发生电话呼叫或其他情况),我希望我的应用程序停止执行任何操作。
活动的所有 on* 方法都是特定于该活动的。我如何监听完全离开我的应用程序的情况?
An android app that I'm writing seems to consume a lot of battery even when it's not the foreground application. My assumption is that I need to unregister the listeners for gps, orientation, etc., when the application is not in the foreground.
The thing is, I have a variety of different activities that I switch between and I don't want the listeners to be unregistered as long as I'm switching between them. However, if the user holds down home and switches to another app (or if a phonecall happens or whatever) I want my app to stop doing anything at all.
All the on* methods of an activity are specific to that activity. How do I listen for switching away from my application entirely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的。
当然可以。每项活动必须是独立的,不能对周围(或不存在)的任何其他活动做出任何假设。如果您愿意,欢迎您将这些内容转移到与您的每项活动绑定的服务中。
Android中没有这个概念。
Correct.
Sure you do. Each activity must stand alone, not making any assumptions about any other activity that is (or is not) around. You are welcome to move this stuff to a service, bound to by each of your activities, if you prefer.
There is no such concept in Android.
您可以在活动的 onpause 时取消注册并在活动的 onresume 时重新注册。这就是我通常处理 GPS 监听器的方式。
You can unregister on the activity's onpause and reregister on the activity's onresume. that's how I usually handle gps listeners.