What is the best practice when polling for sensor values?
I'm working on an application where I periodically need to get a sensor value. For this I'm using AlarmManager and a Receiver at set intervals. Android's sensor implementation seems to be focused around continuous reading of data, and it does not seem possible to poll for the a single current reading.
My question is what the best pra开发者_StackOverflow中文版ctice is for "simulating" a poll mechanism? Are there any examples out there that I haven't come across? One approach is to just call registerListener(), get the current reading, and then call stopListener(). But, this does not seem to work as the reading is not available instantly.
You can use the same approach I used in this project for polling locations. Use a WakeLock
to keep the device awake, and have the service register the sensor listener in onStartCommand()
. When the sensor event arrives, release the WakeLock
and call stopSelf()
to shut down the service.
精彩评论