//***检测传感器
// This code requires API level 3 (Android 1.5) or higher
SensorManager manager = (SensorManager) getSystemService(SENSOR_SERVICE);
boolean isAccelerometerSupported = true;
if(manager.getSensorList(Sensor.TYPE_ACCELEROMETER).isEmpty()){
isAccelerometerSupported = false;
}
//***从传感器(加速度传感器)读取数据
// This code requires API level 3 (Android 1.5) or higher
SensorManager manager;
manager = (SensorManager) getSystemService(SENSOR_SERVICE);
manager.registerListener(new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy){
}
public void onSensorChanged(SensorEvent event){
if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
// The contents of the values[] array depends upon the sensor type.
// See the description of the values[] array in the SensorEvent object documentation.
float azimuth = Math.round(event.values[0]);
float pitch = Math.round(event.values[1]);
float roll = Math.round(event.values[2]);
// Do something with the device orientation values
}
}},
manager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_NORMAL);