これはあなたを助けるかもしれません...
public class SensorActivity extends Activity implements SensorEventListener,
CompoundButton.OnCheckedChangeListener {
private ToggleButton toggleButton;
private SensorManager sensorManager;
private Sensor proximitySensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener(this);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
proximitySensor = sensorManager
.getDefaultSensor(Sensor.TYPE_PROXIMITY);
}
@Override
protected void onResume() {
super.onResume();
if (proximitySensor != null) {
sensorManager.registerListener(this, proximitySensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked && proximitySensor != null) {
sensorManager.registerListener(this, proximitySensor,
SensorManager.SENSOR_DELAY_NORMAL);
} else {
sensorManager.unregisterListener(this);
}
}
}