android 中感測器的使用

weixin_34019929發表於2018-05-05

申明變數

    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);

相關文章