android - SensorEventListener in separate thread -


It looks like a basic question, but after searching for some time and playing with it, I Some help will be appreciated. I want to run a sensor event listener on a separate thread from the UI so that when the event arrives, then computation is required which will not slow the UI

My latest attempt looks like this Is:

class sensor thread thread extended Literacy is {Sensor Manager mSensorManager; Sensor msencer; Public Zero Run () {Log D. ("Routat", Thread.CertTrade (). GetName ()); // thread to display mSensorManager = (sensor manager) getSystemService (SENSOR_SERVICE); MSensor = mSensorManager.getDefaultSensor (sensor.TYPE_ACCELEROMETER); MySensorListener MSL = New MySensor Lasten (); MSensorManager.registerListener (MSL, MS, Sensor Manager. SENSOR_DELAY_UI); } Private class MySensorListener applied sensor event listener {refined change at public zero (sensor sensor, integer accuracy) {} Public Zero sensor changes (sensor events sensor) {log d. ("Listerarag", Thread. Continent (.) GetName ()); // to display the thread}}

Create (or service) the creative (), I create a sensor object and call its start () method. As you expect, the debug log shows the "Routag" entry in the new thread. But the "letterer tag" of Sensor ChangeJet is running in the main thread, even though its object is installed on the new thread. How do I change that?

You can get sensor events in the background thread. Instead

  mSensorManager.registerListener (MSL, mSensor, SensorManager.SENSOR_DELAY_UI);  

You can declare it with a handler in the context of a secondary thread. The thread's running loop looks like this:

  ... run {Looper.prepare; Handler handler = new handler (); ... mSensorManager.registerListener (MSL, MS, Sensor Manager. SENSOR_DELAY_UI, Handler); Looper.loop (); } ...  

Comments