android 中传感器的使用

2018-05-05  本文已影响0人  嵌入式工作

申明变量

    private SensorManager sensor_manage;
    private  Sensor sensorLight;

sensor赋值

       sensor_manage=(SensorManager)getSystemService(SENSOR_SERVICE);
         sensorLight = sensor_manage.getDefaultSensor(Sensor.TYPE_PROXIMITY);//TYPE_ORIENTATION

sensor事件注册,此时调用的类要实现implements SensorEventListener

  sensor_manage.registerListener(this,sensorLight, SENSOR_DELAY_NORMAL);
     @Override
    public void onSensorChanged(SensorEvent sensorEvent) {

     Log.v("test","v0:"+sensorEvent.values[0]+"v1:"+sensorEvent.values[1]+"v2:"+sensorEvent.values[2]+"\r\n");
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

sensor使用结束后取消注册

 sensor_manage.unregisterListener(this);
上一篇下一篇

猜你喜欢

热点阅读