SensorManager.registerListener (.., handler handler), please, an example?

I don’t understand how to use this method,

sensorManager.registerListener (listener SensorEventListener, sensor sensor, int rate, handler handler);

(Documentation here)

1) If it uses a SensorEventListener, then what is the purpose of the handler?

2) Please provide an example of a handler that I could pass to it?

Thank!

+5
source share
3 answers

If it uses a SensorEventListener, then what is the purpose of the handler?

, , (, a HandlerThread). , .

+9

:

SensorManager mSensorMgr = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);

HandlerThread mHandlerThread = new HandlerThread("sensorThread");

mHandlerThread.start();

Handler handler = new Handler(mHandlerThread.getLooper());

mSensorMgr.registerListener(this, mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                    SensorManager.SENSOR_DELAY_FASTEST, handler);

android SensorManager , registerListener() Looper , onSensorChanged.

, SensorManager .

+19

1) If it uses a SensorEventListener, then what is the purpose of the handler? if you run it in the main thread and you do some heavy calculations, you will slow down the main user interface so as not to react. Always write your long-running tasks in a separate thread to avoid ANR.

Here is an example http://stacktips.com/tutorials/android/android-service-example

0
source

All Articles