ANDROID_MARS学习笔记_S05_002_给传感器注册listener
1
1 @Override 2 public void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.main); 5 //获取SensorManager对象 6 sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 7 //获取光线传感器对象 8 Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); 9 10 //注册传感器 11 sensorManager.registerListener(new SensorEventListener() { 12 13 public void onSensorChanged(SensorEvent event) { 14 float acc = event.accuracy; 15 float lux = event.values[0]; 16 System.out.println("acc---->" + acc); 17 System.out.println("lux---->" + lux); 18 } 19 20 public void onAccuracyChanged(Sensor sensor, 21 int accuracy) { 22 23 } 24 }, lightSensor, SensorManager.SENSOR_DELAY_NORMAL); 25 }
You can do anything you set your mind to, man!