Pull a single value from a sensor without using an event

I need help getting information from the orientation sensor. As I saw in almost every tutorial / guide, values ​​are passed to the event (onSensorChanged (SensorEvent event), in which they can be manipulated.

My problem is that I don’t want to keep the electromagnetic / orientation sensor constantly (for the sake of battery life). I want to be able to enable it, capture the current value and disable it. Is there any way to do this?

I did a few searches and found that I can try multithreading, but it’s not very convenient for me.

What I'm looking for is something like (sorry for the lack of formatting, I can't 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 possible, please help me!

Thank you all in advance!

+5
source share
1 answer

When you register a listener for a sensor, activity will be called up every time the sensor values ​​change in accordance with the parameters. Therefore, if you want to receive values ​​only once, what you can do is to cancel the logger for this sensor after receiving the value once.

0
source

All Articles