gpt4 book ai didi

android - 阻止 Android 应用程序关闭

转载 作者:行者123 更新时间:2023-11-30 04:15:20 25 4
gpt4 key购买 nike

我正在使用 BroadcastReceiver 通过我的应用程序接收 SMS 消息,然后根据消息内容编辑数据库。该应用程序在打开时运行良好,但如果我长时间打开它并自动关闭,该应用程序将在收到消息时强制关闭(我认为 BroadcastReciever 仍在工作,但应用程序的其余部分有关闭)。有什么方法可以防止应用在收到短信时关闭或恢复?

谢谢

最佳答案

如果你希望接收器持久化,你应该考虑使用 Service而不是您的应用程序的标准 ActivityBroadcastReceiver存在于标准 Activity 中的 s 被认为是前台服务当处理 onReceive 一旦执行返回 Activity 恢复正常进程优先级,可以根据需要由系统终止。

发件人:BroadcastReceiver

Process Lifecycle

A process that is currently executing a BroadcastReceiver (that is, currently running the code in its onReceive(Context, Intent) method) is considered to be a foreground process and will be kept running by the system except under cases of extreme memory pressure.

Once you return from onReceive(), the BroadcastReceiver is no longer active, and its hosting process is only as important as any other application components that are running in it. This is especially important because if that process was only hosting the BroadcastReceiver (a common case for applications that the user has never or not recently interacted with), then upon returning from onReceive() the system will consider its process to be empty and aggressively kill it so that resources are available for other more important processes.

This means that for longer-running operations you will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation.

有关创建服务的更多信息:

Developer Guides
For a detailed discussion about how to create services, read the Services developer guide.

关于android - 阻止 Android 应用程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10115027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com