gpt4 book ai didi

android - 关闭应用程序时线程是否停止?

转载 作者:行者123 更新时间:2023-12-03 13:04:01 24 4
gpt4 key购买 nike

A service can run in the background indefinitely, even if component that started the service is destroyed.



线程有这个优点吗?

如果我们有一个线程可以从Internet下载文件,那么如果关闭该应用程序,该线程会停止吗?
我知道使用服务更好,但是在将应用程序作为服务关闭后,线程是否可以恢复工作?

最佳答案

在应用程序关闭后,线程确实会继续运行。但是,取决于线程消耗多少资源,操作系统可以随时将其杀死。我不确定您的用例是什么,但是由于线程很可能被杀死,因此您几乎绝对应该使用服务来执行后台任务,例如下载文件。

如果您正在不应该中断的后台线程上进行工作(即使在关闭启动该线程的应用程序之后),请使用服务并在前台启动该服务。这将在通知栏中创建一个图标,通知用户某些后台进程正在运行。这样做使您的服务被杀的可能性大大降低。

从android的文档中:

A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.



Android有关服务的文档是阅读更多内容的好地方:

https://developer.android.com/guide/components/services.html#Foreground

but will threads resume working after the application is closed as services ?



如果线程永远不会被杀死,而您重新打开应用程序,则该线程仍在运行。但同样,由于操作系统很有可能杀死您的线程,因此应将服务用于长时间运行的后台任务。

关于android - 关闭应用程序时线程是否停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37469735/

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