问题描述
这是一个众所周知的问题,许多Android手机的时候。然而在许多手机时,屏幕熄灭步计数停止。有趣的是,计数似乎并没有停止,如果安装了谷歌健身应用(因此我猜测他们保持对加速度计)。
I have tested this on a Samsung S4 and on a Oneplus One and in both cases the steps are counted.How do they do that? What Android classes do they use?My understanding is that the available method introduced since Kitkat is to implement a SensorEventListener. For example theelfismike provides code that implements this. However on many phones the step counting stops when the screen goes off. Interestingly the counting does not seem to stop if the Google Fit app is installed (hence I guess they keep the accelerometer on).
我缺少的东西吗?是保持计数步骤后屏幕关闭提供给程序员凡人的功能?
谢谢!
Am I missing something? Is the functionality of keeping counting steps after screen off available to the mortal programmers?Thanks!
推荐答案
由于ILJA说,你的code运行屏幕被关闭后还是一样。但在这种情况下,我想我们需要一点点不同的答案。
As Ilja said, your code runs even after the screen gets turned off. But in this case I guess we need a little different answer.
他们绝对使用,保持一个服务,他们查询传感器数据。这里重要的部分是抱着激活锁定 - 你必须prevent设备从服务的生命周期内进入睡眠 - 如果你不想错过一些数据
They definitely use a Service that keeps a wakelock and they query the sensors for data. Important part here is holding the wakelock - you have to prevent the device from going into sleep during lifetime of your service - if you don't want to miss some data.
但是,这种方法会耗尽电池真快,因为以检测需要来自传感器的处理相当多的数据的步骤。
But this approach will be drain the battery really fast, because in order to detect steps you need to process quite a lot of data from sensors.
这就是为什么有。这可以让你得到持续的传感器数据,即使没有保持清醒的设备。它基本上存储在芯片本身基于硬件队列权的传感器事件,只有在他们分批predefined的时间间隔发送到您的应用程序(服务..)。这可以让你做一个全天候监控不显著耗尽电池。请注意,只有支持的芯片组可以做到这一点(您可以在Android的文档详细说明),在旧手机的情况下,你需要回退到狰狞激活锁定饲养方法,以获得您的数据。
Thats why there is sensor batching. That allows you to get continuous sensor data even without keeping the device awake. It basically stores the sensor events in a hw based queue right in the chip itself and only sends them to your app (service,..) at predefined intervals in batches. This allows you to do a 24/7 monitoring without draining the battery significantly. Please note that only supported chipsets can do that (you can find details in Android docs), in case of older phones you need to fallback to the hideous wakelock keeping method in order to get your data.
您也可以只使用,但是当有俩都是谷歌适合这种只会工作+谷歌播放设备上安装的服务与监控开启。
You can also just use Google Fit APIs, but this would only work when there're both Google Fit + Google Play Services installed on the device with monitoring turned on.
这篇关于在Android电子计步器:总是在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!