注册传感器
public boolean registerListener(SensorEventListener,
Sensor sensor, int rate)


MainActivity:
package org.gaolei.sensor02;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
public class MainActivity extends Activity {
// 声明一个SensorManager对象
private SensorManager sensorManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取SensorManager对象
sensorManager = (SensorManager) getSystemService(Context.SEARCH_SERVICE);
// 获取光线传感器对象
Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
// 注册传感器
sensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float acc = event.accuracy;
float lux = event.values[0];
System.out.println("acc---->" + acc);
System.out.println("lux---->" + lux);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}