SensorEventListener doesn't get unregistered with unregisterListener() method SensorEventListener doesn't get unregistered with unregisterListener() method android android

SensorEventListener doesn't get unregistered with unregisterListener() method


In my case, to solve this problem I needed to correct the context I was using to get SensorManager. I was inside a Service, and I needed to get the SensorManager this way:

SensorManager sensorManager = (SensorManager) getApplicationContext().getSystemService(SENSOR_SERVICE);

So, just certify if you are getting the SensorManager the correct way.


You don't show enough code to tell for sure, but perhaps your test

if (sensorManager != null && sensorEventListener != null)

simply is not accurate. That is, sensorManager or sensorEventListener may be null when you are still registered as listening.


Try setting the manager to null

sensorManager.unregisterListener(sensorEventListener,sensor);sensorManager = null;

and then obtain the manager again when you need it. This should make the thread finish consistently (it did in my case). I haven't found any documentation explaining this behavior but would be interested in hearing about it.