不使用事件从传感器中提取单个值

发布于 2024-11-02 11:15:51 字数 578 浏览 1 评论 0原文

我需要一些帮助从方向传感器获取信息。正如我在几乎每个教程/指南中看到的那样,这些值被传递给一个事件(onSensorChanged(SensorEvent event)),在其中可以操纵它们。

我的问题是我不想保留电磁/方向传感器不断运行(为了电池寿命),我希望能够打开它,获取当前值并将其关闭,

我已经做了一些搜索,发现我可以 。尝试多线程,但我对此并不完全满意

(抱歉,由于缺乏格式,我似乎无法弄清楚):

private void getOrientationNOW() {
   m_SensorManager.registerListener(mySensorEventListener, m_MagneticSensor, SensorManager.SENSOR_DELAY_FASTEST);
   //---->Something here to get the current value from the sensor
   m_SensorManager.unregisterListener(mySensorEventListener);
}

如果可能,请帮助我!

提前谢谢大家!

I need some help getting info from the orientation sensor. As I have seen in just about every tutorial/guide out there, the values are passed to an event (onSensorChanged(SensorEvent event) in which they can be manipulated.

My problem is that I don't want to keep the electro-magnetic/orientation sensor running constantly (for the sake of battery life). I want to be able to turn it on, grab the current value and switch it off. Is there any way to do this?

I have done some searching and found that I can try multi-threading, but I'm not fully comfortable with that.

What I'm looking for is something like (Sorry for lack of formatting I can't seem to figure it out):

private void getOrientationNOW() {
   m_SensorManager.registerListener(mySensorEventListener, m_MagneticSensor, SensorManager.SENSOR_DELAY_FASTEST);
   //---->Something here to get the current value from the sensor
   m_SensorManager.unregisterListener(mySensorEventListener);
}

If this is possible, please help me!

Thank you all in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-11-09 11:15:51

当您为传感器注册侦听器时,每次传感器值根据参数发生变化时都会调用该活动。因此,如果您只想获取一次值,您可以做的是在获取一次值后取消注册该传感器的侦听器。

When you register a listener for a sensor the activity will be called every time the sensor values changes according to the parameters. So if you want to get the values only one once what you could do is unregister the listener for that sensor after getting the value once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文