不使用事件从传感器中提取单个值
我需要一些帮助从方向传感器获取信息。正如我在几乎每个教程/指南中看到的那样,这些值被传递给一个事件(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您为传感器注册侦听器时,每次传感器值根据参数发生变化时都会调用该活动。因此,如果您只想获取一次值,您可以做的是在获取一次值后取消注册该传感器的侦听器。
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.