SensorEventListener不注销本身 [英] SensorEventListener not unregistering itself
问题描述
我一直在开发一个需要几个步骤进行加速度计校准的应用程序。
I have been developing an application which needs to perform accelerometer calibration in several steps.
我所做的,就是创建一个SensorEventListener,获取一定量的读数,并在达到该数额之后,我称之为'的SensorManager()。unregisterListener(事件监听)。
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)?
推荐答案
如果您已经注册的后您SensorEventListener只被调用一次,那么这很可能的行为是由于的竞争条件,由于Android系统的多线程特性。所以,如果你想关闭处理的读数,然后(如你建议),你应该设置一个布尔变量和if语句,以确保没有进一步的处理测试一旦你已经达到上限。
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.
这篇关于SensorEventListener不注销本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!