38用传感器

一、获取本机所有传感器
SensorManager manager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
List<Sensor> list = manager.getSensorList(Sensor.TYPE_ALL);
二、传感器的使用
1、
SensorManager manager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
2、
private SensorEventListener listener = new SensorEventListener() {


public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub


}


public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub


}
};
3、
onResume()
Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
4、onPause()
manager.unregisterListener(listener);
super.onPause();
5、doSomething()
private void doLock(SensorEvent event) {
int type = event.sensor.getType();
float[] values = event.values;
float x = values[0];
float y = values[1];
float z = values[2];
if (type == Sensor.TYPE_ACCELEROMETER) {
if (x >= VALUE || x <= -VALUE || y >= VALUE || y <= -VALUE
|| z >= VALUE || z <= -VALUE) {

}
}
}

三、11种传感器

#define SENSOR_TYPE_ACCELEROMETER       1 //加速度
#define SENSOR_TYPE_MAGNETIC_FIELD      2 //磁力
#define SENSOR_TYPE_ORIENTATION         3 //方向
#define SENSOR_TYPE_GYROSCOPE           4 //陀螺仪
#define SENSOR_TYPE_LIGHT               5 //光线感应
#define SENSOR_TYPE_PRESSURE            6 //压力
#define SENSOR_TYPE_TEMPERATURE         7 //温度
#define SENSOR_TYPE_PROXIMITY           8 //接近
#define SENSOR_TYPE_GRAVITY             9 //重力
#define SENSOR_TYPE_LINEAR_ACCELERATION 10//线性加速度
#define SENSOR_TYPE_ROTATION_VECTOR     11//旋转矢量

你可能感兴趣的:(android,传感器,sensor)