这是我的代码。我是android开发的新手。我调用registerListener
了按钮单击事件。它开始从加速度计获取数据,但是当我unregisterListener
从另一个按钮单击调用时,它不起作用。
public void startButton(View view){
findViewById(R.id.stop).setEnabled(true);
findViewById(R.id.start).setEnabled(false);
Intent intent = getIntent();
selsensors = intent.getStringExtra(SensorsList.SELECTED_SENSORS);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
senList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccelerometer, mSensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event){
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
ax = event.values[0];
ay = event.values[1];
az = event.values[2];
Toast.makeText(this, "X: "+ax+"\nY: "+ay+"\nZ: "+az, Toast.LENGTH_SHORT).show();
}
}
public void stopButton(View view){
mSensorManager.unregisterListener(this);
this.finish();
}