简体   繁体   English

Android定期传感器数据

[英]Android Periodic Sensor Data

For an Android App, that will show the value of an internal sensor (eg Acceleration) in a graph, i need to find a way to access this signal periodically. 对于Android App,它将在图形中显示内部传感器的值(例如,加速度),我需要找到一种定期访问此信号的方法。 At the moment i am using a SensorEventListener , but unfortunately this only gives me the possibility to get a value whenever it changes. 目前,我正在使用SensorEventListener ,但是不幸的是,这仅使我有可能在值发生变化时获取值。 Since I want to display the graph (point to point) in dependency of the time, this means it would directly draw a line from the old to the new value (and if the old value has been a long time, it looks like a linear changing of the value). 由于我想根据时间显示图形(点对点),这意味着它将直接从旧值绘制一条线到新值(如果旧值已经很长时间了,它看起来像是线性的)值的更改)。 So my question: How can I get access to a sensor's data periodically? 所以我的问题是:如何定期访问传感器的数据?

The documentation for SensorManager says that registerListener(android.hardware.SensorEventListener, android.hardware.Sensor int) SensorManager文档说该registerListener(android.hardware.SensorEventListener, android.hardware.Sensor int)

"Registers a SensorEventListener for the given sensor at the given sampling frequency." “以给定的采样频率为给定的传感器注册一个SensorEventListener。”

To get these events, though, your application would need to be active (hold a partial wake lock). 但是,要获取这些事件,您的应用程序将需要处于活动状态(持有部分唤醒锁)。 It would be better to do this in a background service so that the application doesn't need to remain active. 最好在后台服务中执行此操作,以使应用程序无需保持活动状态。 See for example, SensorEventListener in a service 例如,参见服务中的SensorEventListener

So in the end i used a timer, which checked the values from my sensor, which I put into an array, periodically. 因此,最后我使用了一个计时器,该计时器定期检查来自传感器的值,并将其放入数组中。 Unfortunately registerListener didnt work, since the value is just a suggestion for the system. 不幸的是, registerListener没有起作用,因为该值仅是系统的建议。 Thanks though for the help. 不过谢谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM