一丶传感器
/ /拿到传感器管理器
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE) ; //拿到手机里支持的所有的传感器 List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL) ; for (int i = 0; i < sensors.size(); i++) { Sensor sensor = sensors.get(i) ; System.out.println(sensor.getName()); }eg,光传感器
public class MainActivity extends Activity { private MyListener listener ; private SensorManager sm ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //拿到传感器管理器 sm = (SensorManager) getSystemService(SENSOR_SERVICE) ; //拿到光传感器 Sensor light = sm.getDefaultSensor(Sensor.TYPE_LIGHT) ; listener = new MyListener() ; sm.registerListener(listener, light, SensorManager.SENSOR_DELAY_GAME) ; } private class MyListener implements SensorEventListener{ //强度变化的时候调用 @Override public void onSensorChanged(SensorEvent event) { //拿到光传感器的强度 float f = event.values[0] ; System.out.println(f); } //精度发生变化时调用 @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } } @Override protected void onDestroy() { //注销光传感器的监听器 sm.unregisterListener(listener) ; super.onDestroy(); } }