在Android电子计步器:总是在? [英] Step Counter in Android: always on?

查看:184
本文介绍了在Android电子计步器:总是在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个众所周知的问题,许多Android手机的时候屏幕熄灭关闭加速度。然而事情似乎已经与Android飞度(应用程序)的变化。即使屏幕熄灭飞度一直计数步骤。如果安装了飞度,那么事件上调飞度环境中的步数的计数,我能够用捕捉它们。

It is a well known issue that many Android phones switch off the accelerometer when the screen goes off. However something seems to have changed with Android Fit (the app). Fit keeps counting steps even when the screen goes off. If Fit is installed, then events are raised for step counting within the Fit environment and I am able to capture them using

Fitness.SensorsApi.findDataSources(mClient, new DataSourcesRequest.Builder()
                .setDataTypes(DataType.TYPE_STEP_COUNT_CUMULATIVE)

我已在三星S4和在一加手机并在这两种情况下的步骤进行计数测试此。
他们是怎么做到的?他们使用哪种Android的类?
我的理解是,由于奇巧推出可行的方法是实施SensorEventListener。例如theelfismike提供code的
implements这。然而在许多手机时,屏幕熄灭步计数停止。有趣的是,计数似乎并没有停止,如果安装了谷歌健身应用(因此我猜测他们保持对加速度计)。

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.

您也可以只使用谷歌API的飞度,但是当有俩都是谷歌适合这种只会工作+谷歌播放设备上安装的服务与监控开启。

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电子计步器:总是在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆