• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How best to poll a sensor?

scorpicon

Newbie
Nov 4, 2009
17
0
I'm writing at app that needs to update based on data coming from the orientation sensor. I was able to use the SensorEventListener's onSensorChanged method to continually get the data. However, I only want to poll for the data once every second.

What is the best way to get the sensor data at a specific time interval?

I'm coding for the 2.0 platform, so it'd be nice if any code samples used the current set of APIs. Thanks!
 
Use a timer:
Timer (Java 2 Platform SE v1.4.2)

Register a Java.util.Timer (above link) hat kicks off every 1 second and runs your sensor-checker-method().

Or if you have a main loop, do stuff, sleep, check sensor, loop. I think Loops are discouraged in event-driven programming whenever possible.


I'm writing at app that needs to update based on data coming from the orientation sensor. I was able to use the SensorEventListener's onSensorChanged method to continually get the data. However, I only want to poll for the data once every second.

What is the best way to get the sensor data at a specific time interval?

I'm coding for the 2.0 platform, so it'd be nice if any code samples used the current set of APIs. Thanks!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones