I have very simple Android app: in activity I have a button and I start/stop the OrientationListener. However, after unregistering it, in ddms I can still see the thread
//Declaration of SensorManager
private SensorManager sensorManager;
private Sensor mAccelerometer;
private SensorEventListener sensorEventListener;
onCreate call To
try {
sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(sensorEventListener=new SensorEventListener() {
int orientation = -1;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[1] < 6.5 && event.values[1] > -6.5) {
if (orientation != 1) {
Log.d("Sensor", "Landscape");
}
orientation = 1;
} else {
if (orientation != 0) {
Log.d("Sensor", "Portrait");
}
orientation = 0;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
},
mAccelerometer=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
} catch (Exception e) {
e.printStackTrace();
}
Please call to on Destroy
if (sensorManager != null && mAccelerometer != null)
{
sensorManager.unregisterListener(sensorEventListener, mAccelerometer); sensorManager = null;
}
Could be a problem with scope. Try logging the values of sensorEventListener and sensor when you register and unregister them. (.toString()) to make sure that they are the same.
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.
I faced similar issue.
Workaround to stop the sensor.
I used a static boolean mIsSensorUpdateEnabled. Set it to 'false' when you want to stop getting values from sensors.
And in onSensorChanged() method, check the value of the boolean variable and again make to call to unregister the sensors. And this time, it works. The sensors would be unregistered and you will no longer get 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
}
}
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.
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.