标签:android private listener public 传感器
一丶传感器
/ /拿到传感器管理器
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();
}
}
本文出自 “android笔记” 博客,请务必保留此出处http://2585211.blog.51cto.com/10044233/1673831
标签:android private listener public 传感器
原文地址:http://2585211.blog.51cto.com/10044233/1673831