Android listener not receiving unregistered data or sensor still retrieving values

I have some problems with SensorManager.unregisterListener. They are similar to Android, Thread, Can not unregister SensorEventListener and SensorEventListener does not receive the unregistered unregisterListener () method . But I could not find my answer! I also took advantage of the SensorManager documentation example http://developer.android.com/reference/android/hardware/SensorManager.html#SENSOR_DELAY_UI .

The problem is that after the completion of the Activity, I unregistered the listener, but the sensor continues to send values ​​until the application is completely destroyed.

This is my code:

my SensorController implements a SensorEventListener

public class SensorController implements SensorEventListener{ 

Constructor:

 public SensorController(Context context){ sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); } 

my SensorController onResume method is called when the Activity onResume callback is called:

 public void onResume(){ mAccelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); boolean ret1 = sensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_UI); mMagneticSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); boolean ret2 = sensorManager.registerListener(this, mMagneticSensor, SensorManager.SENSOR_DELAY_UI); } 

my SensorController onPause method is called when the Activity onPause callback is called:

 public void onPause(){ sensorManager.unregisterListener(this); } 

And in SensorChanged, I put a log to monitor sensor events:

 @Override public void onSensorChanged(SensorEvent event) { this.computeRotationAngles(event); Log.i("SensorController", "onSensorChanged"); } 

So ... after the Activity onPause method, the onSensorChanged (SensorEvent) event still retrieves the sensor events. What for?? I unregistered the SensorController.onPause () function. Sensors extract good values, but I can't stop sending them. What am I doing wrong?

Thank you in advance! I will be grateful for every little advice :)

+3
source share
2 answers

try uninstalling the application and reinstalling it. he works on mine.

+1
source

I ran into a similar problem.

Workaround to stop the sensor from draining the battery.

I used the static boolean mIsSensorUpdateEnabled . Set to false when you want to stop receiving values ​​from sensors. And in the onSensorChanged () method, check the value of the boolean variable and make a call again to unregister the sensors. And this time it works. Sensors will be unregistered and you will no longer receive the onSensorChanged callback.

 public class MainActivity extends Activity implements SensorEventListener { private static boolean mIsSensorUpdateEnabled = false; private SensorManager mSensorManager; private Sensor mAccelerometer; @override protected void onCreate(){ mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); } private startSensors(){ mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); int delay = 100000; //in microseconds equivalent to 0.1 sec mSensorManager.registerListener(this, mAccelerometer, delay ); mIsSensorUpdateEnabled =true; } private stopSensors(){ mSensorManager.unregisterListener(this, mAccelerometer); mIsSensorUpdateEnabled =false; } @Override public void onSensorChanged(SensorEvent event) { if (!mIsSensorUpdateEnabled) { stopSensors(); Log.e("SensorMM", "SensorUpdate disabled. returning"); return; } //Do other work with sensor data } } 
+1
source

Source: https://habr.com/ru/post/1206951/


All Articles