Android 检查应用程序状态

发布于 2024-11-30 00:21:43 字数 99 浏览 1 评论 0原文

我对 Android 应用程序状态有疑问。我想知道我是否能够检查应用程序是活着还是死了、可见还是在后台。我需要根据我的广播接收器中的应用程序状态做出决定。

提前致谢..

I have a question about Android application status. I want to know that am I able to check whether application is alive or dead, visible or on background. I need to make a decision according to application status in my broadcast receiver.

Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

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

评论(3

我很OK 2024-12-07 00:21:43

您可以通过查看“启动过程”中加载的数据是否存在来确定您的“应用程序”是否“活动”。如果有,请使用它。如果没有,请运行“启动过程”。

You determine if your "application" is "alive" by seeing if the data you loaded in your "start up process" exists. If it does, use it. If it does not, run the "start up process".

傲性难收 2024-12-07 00:21:43

基本上,当您接收广播时,您的应用程序就可以保证运行。这是因为如果不先启动应用程序进程就无法接收广播。如果发送广播时没有活动的组件(如活动),则启动应用程序进程的唯一目的是处理该广播。

至于另一部分,检测当前是否有任何 Activity 处于活动状态和/或可见 - 我还没有看到任何对此的 API 调用。可能有一些解决方法(例如手动将计数器保持在单例中),但据我所知,没有对此的直接支持。虽然我可能是错的。

Basically, when your receive broadcast your application is guaranteed to run. That's because broadcast can not be received without application process started first. And if there were no components active (like activities) when broadcast is sent, then application process is started for the sole purpose of processing this broadcast.

As for the other part, detecting if any Activity is currently active and/or visible - I haven't seen any API calls for this. There are might be some workaround (like manually keeping the counter in a singleton), but as far as I know there is no direct support for this. Though I might be wrong.

一身骄傲 2024-12-07 00:21:43

在 Android 中,跟踪各种状态的“变化”是标准做法。您可以在此处了解更多相关信息。在特定生命周期相关方法中,您可以根据需要执行特定操作。

Within Android, it is standard practise to track 'changes' to the various states. You can read more about this here. Within the specific lifecycle related methods, you can perform specific actions as deemed appropriate.

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