在 Android 中持续监控传感器
我正在尝试找出以小于 0.25 毫秒的轮询率监控加速计传感器的最佳方法。我已经实现了一个 UI 选项,供用户切换到持续监控状态,并明确了电池消耗的影响。由于 Android 处理清理内存和线程的方式,远程服务是否是比守护线程更好的方法?关键是要让加速度计的监测尽可能接近持续,电池耗尽是该死的。而且这个监控需要长时间运行,甚至可能连续超过24小时,我再次意识到功耗的后果。任何建议的阅读或代码片段将不胜感激。
只是一个寻求 Android 社区智慧建议的新手。提前致谢,
-Steve
澄清:我正在尝试检测加速度发生变化的瞬间。我的代码按轴进行区分,但从加速度计获取实时数据是我的目标。
I am trying to figure out the best way to monitor the accelerometer sensor with a polling rate of less than .25 milliseconds. I have implemented a UI option for the user to switch to a constant monitoring state and made it clear of the battery drain ramifications. Would a remote service be the best way over a daemon thread because of the way Android handles cleaning up memory and threads? The point is to make the accelerometer monitored as close to constantly as possible, battery drain be damned. And this monitoring needs to be long running, maybe even more than 24 hours straight, again I realize the power consumption consequences. Any suggested reading or code snippets will be appreciated.
Just a newbe looking for advice from the wisdom of the Android community. Thanks in advance,
-Steve
CLARIFICATION: I am trying to detect the instant there is a change in acceleration. My code discriminates by axis, but getting real time data from the accelerometer is my goal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们使用 Android Services 来完成此操作 - 它们可以从 Activity 启动,但是保持在后台运行。这可能就是您正在寻找的!
一些 Howtos:
We did this using Android Services - they can be started from an activity but remain running in the background. That's probably what you're looking for!
Some Howtos:
使用特定线程来监视和等待是为您提供等待时间灵活性的最佳解决方案。这是非常有效的,因为它不需要任何特定的服务。
请参阅http://developer.android.com/reference/java/lang/Thread。 html
您指定了一个非常短的延迟(0.250 毫秒),因此这将是 CPU 密集型的。
您可以使用加速度计的结果来增加或减少此延迟。
例如,如果您检测到没有加速,则增加延迟(它是
对你来说,但 100 毫秒似乎是合理的,甚至更高)。一旦发现某些情况,请减少
延迟。所有这些都取决于您的应用程序。
Using a specific thread to monitor and wait is the best solution that gives you flexibility on the wait period. This is quite efficient as it does not requires any specific service.
See http://developer.android.com/reference/java/lang/Thread.html
You specify a very short delay (.250 ms) so this will be CPU intensive.
You can probably use the result of the accelerometer to increase or reduce this delay.
For example, if you detect that there is no acceleration, increase the delay (it's up
to you but 100ms seems reasonable or even higher). As soon you detect something, reduce
the delay. All this depends on your application.