문제

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 practice 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top