SensorEventListener not unregistering itself

[亡魂溺海] 提交于 2019-12-11 16:35:52

问题


I have been developing an application which needs to perform accelerometer calibration in several steps.

What I have done, is create a SensorEventListener that gets a certain amount of readings, and after that amount is reached, I call 'sensorManager().unregisterListener(eventListener)'.

However, I think this line is not unregistering the listener, since immediately afterwards, the same line is called, as a result of continuing to run.

Question: am I doing things properly this way, or should condition my handler so that after reaching this condition it doesn't allow itself to run (using if's)?


回答1:


If your SensorEventListener only gets called once after you've unregistered it then this behaviour is probably due to a race condition, due to the multi-threaded nature of Android. So if you want to shut off processing the readings, then (as you suggest) you should set a boolean variable and test it with if statements to ensure no further processing once you've reached your limit.



来源:https://stackoverflow.com/questions/15947910/sensoreventlistener-not-unregistering-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!