Android:前台服务与wakeLock

发布于 2025-01-02 04:52:39 字数 236 浏览 1 评论 0原文

我对 Android 比较陌生,所以我问的问题可能看起来很明显(尽管我已经阅读了所有类似标题的问题,并且进行了广泛的搜索)。我需要长时间连续监控加速度计。建议采用两种方法:

1)获取在加速器被监控的整个过程中保持的部分唤醒锁; 2

) 在前台服务中监视加速度计。

第一种方法似乎会消耗大量电池寿命。第二种方法应该会导致服务很少被杀死,但我不确定“很少”是什么意思。应该使用哪种方法?我应该考虑哪些替代方法?

I am relatively new to Android, so what I am asking may seem obvious (although I have read all the similarly titled questions, and have searched extensively). I need to monitor the accelerometer continuously for long periods. Two approaches have been suggested:

1) acquire a partial wake lock that is held the entire time the acceleromtere is being monitored; and

2) monitor the accelerometer in a foreground service.

The first approach appears to use a lot of battery life. The second approach should result in a service that is only killed rarely, but I'm not sure what "rarely" means. Which approach should be used, and are there alternatives that I should consider?

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

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

发布评论

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

评论(2

落花浅忆 2025-01-09 04:52:39

持有 WakeLock 和前台 Service 并没有真正的相关性,也不应该比较哪个方向最好。

Android 操作系统旨在根据多种因素交换进程。这意味着您的进程可能随时被 Android 终止,它提供了一个框架来帮助您(开发人员)确保您的应用程序可以在发生这种情况时保存和恢复其状态。

WakeLock 只是防止 CPU 休眠,这有助于在手机不使用时节省电池电量。

现在,两者的结合将帮助您实现您想要的目标,但用户成本却很高。我不希望手机中的应用程序保持 CPU 持续运行,也不希望通知图标不断显示在通知栏中(这就是前台服务的作用)。

请记住,在前台模式下启动服务并不能保证您的应用程序不会被终止。尽管很少,但它仍然可能发生。

您想在这里实现什么目标?为什么要持续监控设备加速度计?也许您应该只在应用的 Activity 位于前台时才监控它。

Holding a WakeLock and a foreground Service are not really related and shouldn't be compared are to which direction is best.

Android OS is built to swap out processes based on a variety of factors. This means your process might get killed at any point by Android and it provides a framework to help you, the developer, to ensure your app can save and restore its state when this happens.

A WakeLock simply prevents the CPU from sleeping which helps save battery when the phone is not in use.

Now, a combination of both would help you achieve what you want but at great user cost. I wouldn't want an app in my phone to keep the CPU constantly running or a notification icon to show up constantly in the notification bar (that's what a foreground service does).

Keep in mind, starting a service in foreground mode does not guarantee your app will not get killed. It might still happen albeit rarely.

What is it you are trying to achieve here? Why keep monitoring the devices accelerometer? Perhaps you should only monitor it only when an Activity of your app is in the foreground instead.

不如归去 2025-01-09 04:52:39

我有完全相同的需求和问题。我相信解决方案是同时使用部分唤醒锁和前台服务。 Android 将尝试不终止持有唤醒锁的后台服务,但可以在需要资源时随意终止它,并可能稍后重新启动它。这对于很多目的来说都很好,但至少就我而言还不够好。将服务置于前台状态是告诉 Android 杀死它是不可接受的方式。是的,在极端情况下它仍然可能发生,但这现在违反了 API 合同,而使用后台服务 Android 可以随意终止它。因此,您应该编码就好像这种情况永远不会发生一样,但只知道这是一个可能但可能罕见的错误。

I had exactly the same need and problem. I believe the solution is to use both a partial wake lock and a foreground service. Android will try not to kill a background service that holds a wake lock but is free to kill it when it needs the resources and possibly restart it later. That's fine for a lot of purposes but at least in my case that is not good enough. Putting a service into the foreground state is the way to tell Android that that killing it is unacceptable. Yes, it might still happen in extreme situations but that would now be a violation of the API contract whereas with a background service Android is free to kill it. You should therefore probably code as if that that will never happen but just know that this is a possible but probably rare error.

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