轮询传感器值时的最佳实践是什么?
我正在开发一个应用程序,我需要定期获取传感器值。为此,我按设定的时间间隔使用 AlarmManager 和接收器。 Android 的传感器实现似乎专注于数据的连续读取,并且似乎不可能轮询单个当前读数。
我的问题是“模拟”轮询机制的最佳实践是什么?还有我没有遇到过的例子吗?一种方法是仅调用 registerListener(),获取当前读数,然后调用 stopListener()。但是,这似乎不起作用,因为无法立即读取内容。
I'm working on an application where I periodically need to get a sensor value. For this I'm using AlarmManager and a Receiver at set intervals. Android's sensor implementation seems to be focused around continuous reading of data, and it does not seem possible to poll for the a single current reading.
My question is what the best practice is for "simulating" a poll mechanism? Are there any examples out there that I haven't come across? One approach is to just call registerListener(), get the current reading, and then call stopListener(). But, this does not seem to work as the reading is not available instantly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用我在此项目中使用的相同方法来投票位置。使用
WakeLock
使设备保持唤醒状态,并让服务在onStartCommand()
中注册传感器侦听器。当传感器事件到达时,释放WakeLock
并调用stopSelf()
关闭服务。You can use the same approach I used in this project for polling locations. Use a
WakeLock
to keep the device awake, and have the service register the sensor listener inonStartCommand()
. When the sensor event arrives, release theWakeLock
and callstopSelf()
to shut down the service.