SensorEventListener in separate thread

A little late, but if others still want to know, here is a good way to achieve this. As always when multithreading, make sure you know what you are doing and take the time to so it right, to avoid those weird errors. Have fun!

Class members:

private HandlerThread mSensorThread;
private Handler mSensorHandler;

in OnCreate or when registering:

mSensorThread = new HandlerThread("Sensor thread", Thread.MAX_PRIORITY);
mSensorThread.start();
mSensorHandler = new Handler(mSensorThread.getLooper()) //Blocks until looper is prepared, which is fairly quick
yourSensorManager.registerListener(yourListener, yourSensor, interval, mSensorHandler);

When unregistering, also do:

mSensorThread.quitSafely();

Leave a Comment