从具有唤醒锁的服务运行的线程是否需要唤醒锁?

发布于 2024-12-29 06:24:25 字数 189 浏览 2 评论 0原文

我想运行一个线程在后台执行一些工作(从获取唤醒锁的服务)。当我的线程完成工作后,它将停止服务。

我的担忧是:

1-我是否需要在服务中启动的线程中要求唤醒锁?

2-系统可以在保持服务运行的同时终止我的线程吗?

3-如果是2的情况,我怎样才能停止我的服务(我可以给服务一个超时时间,让它在之后终止)

I want to run a thread to do some work in the background (from a service that acquired a wakelock). after my thread finish work then it will stop the service.

My concerns are :

1- Do i need to require a wakelock in the thread that was started in the service?

2- Can the system kill my thread while leaving my service running?

3- if 2 is the case how could i stop my service (can i give the service a timeout time that it will die after it)

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

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

发布评论

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

评论(1

一个人的旅程 2025-01-05 06:24:25
  1. 据我了解,WakeLocks 应用于 Android 组件(例如服务、活动等)及其所有关联线程。因此,您不必在线程中获取新的唤醒锁。您可以通过传递获取的锁并从启动的线程调用 isHeld 来证明这一点。

  2. 您应该考虑部分唤醒锁,这仅涉及保持 CPU 运行,而不管屏幕或键盘活动如何。由于这是一项服务,我假设您不关心屏幕是否保持打开状态。

请注意,此处指出:如果您持有部分唤醒锁,CPU无论任何计时器如何,甚至在用户按下电源按钮后,都会继续运行。在所有其他唤醒锁中,CPU 将运行,但用户仍然可以使用电源按钮使设备进入睡眠状态。

编辑:另外,即使您可以按照您建议的方式执行此操作,但出于电池消耗的目的,最好在子线程中获取和释放唤醒锁。如果您需要使用 WiFi 来完成这项工作,则可能还需要 WiFiLock。

  1. WakeLocks are, as I understand applied to the Android component (e.g. service, activity, etc.) and all its associated threads. So you would not have to aquire a new wakelock in your thread. You could prove this by passing the aquired lock and calling isHeld from the started thread.

  2. You should consider a partial wakelock, this will concern only keeping the CPU running, regardless of the screen or keyboard activity. Since this is a service I assume you don't care whether the screen stays on.

Note that it states here: If you hold a partial wakelock, the CPU will continue to run, irrespective of any timers and even after the user presses the power button. In all other wakelocks, the CPU will run, but the user can still put the device to sleep using the power button.

Edit: Also, even though you can do it the way you suggested, it might be better to only aquire and release the wakelock in the child thread for battery consumption purposes. And if you are needing to use WiFi for this work a WiFiLock may also be required.

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