我想在执行某些操作后停止传感器管理器活动(从其他线程或类中取消注册)。我怎么能这样做?
答案 0 :(得分:1)
看这里。它可能会对你有帮助。
ShakeListener.java
Made Instance variable in ShakeListener class.
private Sensor accelerometer;
SensorManager sensorManager;
public ShakeListener(Context context) {
sensorManager = (SensorManager)this.context.getSystemService(Context.SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
从其他线程或类中取消注册。
这样做:
private ShakeListener shakeListener; //Instance variable
shakeListener = new ShakeListener(this);
shakeListener.sensorManager.unregisterListener(shakeListener, shakeListener.sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));