Android 为后台作业使用线程
Android Using a Thread for Background Jobs
我读过很多帖子说打瞌睡模式在特定时刻杀死了一个 运行 服务 e.x link 或者他们想要执行一个长的 运行 线程.
我不明白为什么你应该使用一个服务来做一个你知道它最终会停止的后台工作。
例如:
您可以使用简单的线程:
new Thread(new Runnable).start()
并在其中做一些工作。使用这个:
- 结合唤醒锁,设备不会休眠,线程将保持 运行。
- 没有打瞌睡模式限制(网络除外,但可以说我们做本地的事情)
因此您可以不受任何限制地进行后台工作。尽管出于这些原因您应该使用服务 .
这是做后台工作的另一种方式吗(当然不是更好,但仍然是一种方式)?我错了吗?
除了服务之外,还有很多方法可以完成后台工作,请检查此 link 它可能会帮助您选择最适合您工作的选项:
Job Scheduler vs Background Service
@TheWanderer 所说的服务将在应用程序关闭一段时间后继续工作,这与应用程序关闭时将立即结束的简单线程不同。
阅读您link编辑
的link中的这一部分
Services are given higher priority than other Background processes and
hence it’s less likely that Android will terminate it. Although it can
be configured to restart once there is ample resources available
again. You should go through the different processes and their
priority/important level in the documentation on processes and
threads. Assigning them the same priority as foreground activities is
definitely possible in which case it’ll need to have a visible
notification active (generally used for Services playing music).
如果您 运行 从 Activity
启动后台线程,Android 不知道您正在 OS 进程中进行后台工作那就是托管您的 Activity
。 Android 几乎可以随时终止托管您的 Activity
的 OS 进程。如果用户按下主页按钮或接听 phone 电话或打开通知并转到另一个应用程序,Android 可以随时终止 OS 进程。当用户 returns 到您的应用程序时,Android 将创建一个新的 OS 进程并重新创建所有相关活动,但您的后台线程无可救药地丢失了。这就是Android有服务的原因。
如果您启动 Service
来执行您的后台处理,Service
也会启动后台线程,但这些线程是受控的。你的 Service
告诉 Android 如果它在处理 Intent
时杀死了 Service
该怎么办。因此可以通知您的 Service
并在必要时重新启动(或继续)后台处理。您还可以 运行 Service
在与 OS 流程 运行 活动不同的 OS 流程中。如果用户从最近的任务列表中删除您的应用程序,这将防止 Android 终止 Service
。
有了较新的 Android SDK,您还可以使用其他机制,例如 JobScheduler
。
我读过很多帖子说打瞌睡模式在特定时刻杀死了一个 运行 服务 e.x link 或者他们想要执行一个长的 运行 线程.
我不明白为什么你应该使用一个服务来做一个你知道它最终会停止的后台工作。
例如:
您可以使用简单的线程:
new Thread(new Runnable).start()
并在其中做一些工作。使用这个:
- 结合唤醒锁,设备不会休眠,线程将保持 运行。
- 没有打瞌睡模式限制(网络除外,但可以说我们做本地的事情)
因此您可以不受任何限制地进行后台工作。尽管出于这些原因您应该使用服务
这是做后台工作的另一种方式吗(当然不是更好,但仍然是一种方式)?我错了吗?
除了服务之外,还有很多方法可以完成后台工作,请检查此 link 它可能会帮助您选择最适合您工作的选项: Job Scheduler vs Background Service
@TheWanderer 所说的服务将在应用程序关闭一段时间后继续工作,这与应用程序关闭时将立即结束的简单线程不同。
阅读您link编辑
的link中的这一部分Services are given higher priority than other Background processes and hence it’s less likely that Android will terminate it. Although it can be configured to restart once there is ample resources available again. You should go through the different processes and their priority/important level in the documentation on processes and threads. Assigning them the same priority as foreground activities is definitely possible in which case it’ll need to have a visible notification active (generally used for Services playing music).
如果您 运行 从 Activity
启动后台线程,Android 不知道您正在 OS 进程中进行后台工作那就是托管您的 Activity
。 Android 几乎可以随时终止托管您的 Activity
的 OS 进程。如果用户按下主页按钮或接听 phone 电话或打开通知并转到另一个应用程序,Android 可以随时终止 OS 进程。当用户 returns 到您的应用程序时,Android 将创建一个新的 OS 进程并重新创建所有相关活动,但您的后台线程无可救药地丢失了。这就是Android有服务的原因。
如果您启动 Service
来执行您的后台处理,Service
也会启动后台线程,但这些线程是受控的。你的 Service
告诉 Android 如果它在处理 Intent
时杀死了 Service
该怎么办。因此可以通知您的 Service
并在必要时重新启动(或继续)后台处理。您还可以 运行 Service
在与 OS 流程 运行 活动不同的 OS 流程中。如果用户从最近的任务列表中删除您的应用程序,这将防止 Android 终止 Service
。
有了较新的 Android SDK,您还可以使用其他机制,例如 JobScheduler
。