| 1 | 检测传感器 | ||||||||||||||||||||||||||||||||||||||||||||
| 权限 | |||||||||||||||||||||||||||||||||||||||||||||
| 代码 | // This code requires API level 3 (Android 1.5) or higher | ||||||||||||||||||||||||||||||||||||||||||||
| SensorManager manager = (SensorManager) getSystemService(SENSOR_SERVICE); | |||||||||||||||||||||||||||||||||||||||||||||
| boolean isAccelerometerSupported = true; | |||||||||||||||||||||||||||||||||||||||||||||
| if(manager.getSensorList(Sensor.TYPE_ACCELEROMETER).isEmpty()){ | |||||||||||||||||||||||||||||||||||||||||||||
| isAccelerometerSupported = false; | |||||||||||||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||||||||||||
| 2 | 从传感器(加速度传感器)读取数据 | ||||||||||||||||||||||||||||||||||||||||||||
| 权限 | |||||||||||||||||||||||||||||||||||||||||||||
| 代码 | // This code requires API level 3 (Android 1.5) or higher | ||||||||||||||||||||||||||||||||||||||||||||
| SensorManager manager; | |||||||||||||||||||||||||||||||||||||||||||||
| manager = (SensorManager) getSystemService(SENSOR_SERVICE); | |||||||||||||||||||||||||||||||||||||||||||||
| manager.registerListener(new SensorEventListener(){ | |||||||||||||||||||||||||||||||||||||||||||||
| public void onAccuracyChanged(Sensor sensor, int accuracy){ | |||||||||||||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||||||||||||
| public void onSensorChanged(SensorEvent event){ | |||||||||||||||||||||||||||||||||||||||||||||
| if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){ | |||||||||||||||||||||||||||||||||||||||||||||
| // The contents of the values[] array depends upon the sensor type. | |||||||||||||||||||||||||||||||||||||||||||||
| // See the description of the values[] array in the SensorEvent object documentation. | |||||||||||||||||||||||||||||||||||||||||||||
| float azimuth = Math.round(event.values[0]); | |||||||||||||||||||||||||||||||||||||||||||||
| float pitch = Math.round(event.values[1]); | |||||||||||||||||||||||||||||||||||||||||||||
| float roll = Math.round(event.values[2]); | |||||||||||||||||||||||||||||||||||||||||||||
| // Do something with the device orientation values | |||||||||||||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||||||||||||
| }}, | |||||||||||||||||||||||||||||||||||||||||||||
| manager.getDefaultSensor(Sensor.TYPE_ORIENTATION), | |||||||||||||||||||||||||||||||||||||||||||||
| SensorManager.SENSOR_DELAY_NORMAL); | |||||||||||||||||||||||||||||||||||||||||||||
Android 常用代买---传感器
最新推荐文章于 2025-11-10 13:35:36 发布
1万+

被折叠的 条评论
为什么被折叠?



