/*获取手机上所有传感器的信息*/
private void getAllSensor() {
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//获取手机上支持的所有传感器
List<Sensor> mList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
for (Sensor sensor : mList) {
Log.e("KeithXiaoY", "名字:" + sensor.getName());
Log.e("KeithXiaoY", "type:" + "(" + sensor.getType() + ")");
Log.e("KeithXiaoY", "vendor:" + sensor.getVendor());
Log.e("KeithXiaoY", "version:" + sensor.getVersion());
Log.e("KeithXiaoY", "resolution:" + sensor.getResolution());
Log.e("KeithXiaoY", "max range:" + sensor.getMaximumRange());
Log.e("KeithXiaoY", "power:" + sensor.getPower());
if (sensor.getType() == 5) {
mSensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
Toast.makeText(getApplicationContext(), "光线变化", Toast.LENGTH_LONG).show();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sensor, SensorManager.SENSOR_DELAY_FASTEST);//SensorManager.SENSOR_DELAY_FASTEST这个表示最即时的,另外三个,参照SensorManager中的定义
}
}
}
关于Android中的传感器
最新推荐文章于 2023-05-05 22:11:42 发布