Android:前台服务与WakeLock

编程入门 行业动态 更新时间:2024-10-18 18:28:57
本文介绍了Android:前台服务与WakeLock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对Android来说还比较陌生,所以我要问的内容似乎很明显(尽管我已经阅读了所有类似标题的问题,并且进行了广泛搜索).我需要长时间连续监测加速度计.提出了两种方法:

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)获取部分唤醒锁,该锁在整个监测加速度计的过程中保持不变;和

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

2)监视前台服务中的加速度计.

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?

推荐答案

保持WakeLock和前景Service并没有真正的关联,不应将其与哪个方向最好进行比较.

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

Android OS旨在根据各种因素交换出进程.这意味着您的进程可能会在任何时候被Android杀死,并且它提供了一个框架来帮助您(开发人员)确保发生这种情况时您的应用可以保存和恢复其状态.

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.

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

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

现在,将两者结合使用可以帮助您实现所需的目标,但需要付出高昂的用户成本.我不希望手机中的应用程序保持CPU持续运行,也不希望通知图标始终显示在通知栏中(这是前台服务所做的事情).

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.

您想要在这里实现什么?为什么要继续监视设备加速度计?也许只应在应用程序的Activity位于前台时才监视它.

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.

更多推荐

Android:前台服务与WakeLock

本文发布于:2023-11-27 08:00:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637190.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前台   Android   WakeLock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!