Android编程中光线传感器的调用方法详解

  public class TestActivity extends Activity {

  private SensorManager sensorManager;

  //第三步:对传感器信号进行监听

  private SensorEventListener listener = new SensorEventListener() {

  @Override

  public void onSensorChanged(SensorEvent event) {

  //提示当前光照强度

  Toast.makeText(TestActivity.this,

  "当前光照强度:" + event.values[0] + "勒克斯", Toast.LENGTH_SHORT).show();

  }

  @Override

  public void onAccuracyChanged(Sensor sensor, int accuracy) {

  }

  };

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  //第一步:获取 SensorManager 的实例

  sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

  //第二步:获取 Sensor 传感器类型

  Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

  //第四步:注册 SensorEventListener

  sensorManager.registerListener(listener,sensor,SensorManager.SENSOR_DELAY_NORMAL);

  }

  @Override

  protected void onDestroy() {

  super.onDestroy();

  //传感器使用完毕,释放资源

  if(sensorManager!=null){

  sensorManager.unregisterListener(listener);

  }

  }

  }