我的 Android 应用程序不在屏幕上时应暂停活动

发布于 2024-11-27 00:15:08 字数 255 浏览 2 评论 0原文

我正在编写的一个 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技术交流群

发布评论

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

评论(2

七秒鱼° 2024-12-04 00:15:08

我的假设是,当应用程序不在前台时,我需要取消注册 GPS、方向等侦听器。

正确的。

问题是,我有多种不同的活动可以在它们之间切换,并且只要我在它们之间切换,我就不希望监听器被注销。

当然可以。每项活动必须是独立的,不能对周围(或不存在)的任何其他活动做出任何假设。如果您愿意,欢迎您将这些内容转移到与您的每项活动绑定的服务中。

我如何监听完全离开我的应用程序的情况?

Android中没有这个概念。

My assumption is that I need to unregister the listeners for gps, orientation, etc., when the application is not in the foreground.

Correct.

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.

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.

How do I listen for switching away from my application entirely?

There is no such concept in Android.

錯遇了你 2024-12-04 00:15:08

您可以在活动的 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.

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