目前常用的有两种(小三也只有两种)
加速传感器:SENSOR_ACCELEROMETER
姿态传感器:SENSOR_ORIENTATION
经试验
public void onSensorChanged(int sensor, float[] values) {
里的values为如下含义;
当传感器为姿态传感器(SENSOR_ORIENTATION)时values的含义如下:
SENSOR_ORIENTATION
, SENSOR_ORIENTATION_RAW
:
All values are angles in degrees.
values[0]: Azimuth, rotation around the Z axis (0<=azimuth<360). 0 = North, 90 = East, 180 = South, 270 = West
values[1]: Pitch, rotation around X axis (-180<=pitch<=180), with positive values when the z-axis moves toward the y-axis.
values[2]: Roll, rotation around Y axis (-90<=roll<=90), with positive values when the z-axis moves toward the x-axis.
values[0] 为Yaw轴上的转动角度
values[1] 为Pitch轴上的转动角度
values[2] 为Roll轴上的转动角度
加传感器相应,首先在onCreate里获得系统的sensorsevice
s_sensor = (SensorManager)getSystemService(SENSOR_SERVICE);
之后再Activity类下的onResume里设定监听
int mask = 0; mask |= SensorManager.SENSOR_ORIENTATION;//姿态传感器 //mask |= SensorManager.SENSOR_ACCELEROMETER;//加速度传感器 s_sensor.registerListener(mListener, mask, SensorManager.SENSOR_DELAY_GAME);
然后,在创建监听接口
public static float s_sensorValue[] = new float[3]; private final SensorListener mListener = new SensorListener() { @Override public void onAccuracyChanged(int sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(int sensor, float[] values) { // TODO Auto-generated method stub if(sensor == SensorManager.SENSOR_ORIENTATION) { s_sensorValue[0] = values[0]; s_sensorValue[1] = values[1]; s_sensorValue[2] = values[2]; // System.out.println("value[0] = " + values[0]); // System.out.println("value[1] = " + values[1]); // System.out.println("value[2] = " + values[2]); } } };
之后就可以根据获得的s_sensorValue去进行传感器相应啦。