Android基于Sensor感应器获取重力感应加速度的方法

2019-12-10 19:41:35王振洲

int     TYPE_ORIENTATION     This constant is deprecated. use SensorManager.getOrientation() instead.
//方向感应器 已过时 可以使用方法获得
int     TYPE_PRESSURE     A constant describing a pressure sensor type                            
//压力感应器  单位 千帕斯卡
int     TYPE_PROXIMITY     A constant describing an proximity sensor type.                         
//距离传感器
int     TYPE_ROTATION_VECTOR     A constant describing a rotation vector sensor type.            
//翻转传感器
int     TYPE_TEMPERATURE     A constant describing a temperature sensor type                 
//温度传感器 单位 摄氏度

此类中包含的方法都是get型的 用来获取所选sensor的一些属性,sensor类一般不需要new而是通过SensorManager的方法获得

二 介绍SensorManager类

SDK解释:“SensorManager lets you access the device's sensors. Get an instance of this class by calling Context.getSystemService() with the argument SENSOR_SERVICE.
Always make sure to disable sensors you don't need, especially when your activity is paused. Failing to do so can drain the battery in just a few hours. Note that the system will not disable sensors automatically when the screen turns off. ”
SensorManager 允许你访问设备的感应器。通过传入参数SENSOR_SERVICE参数调用Context.getSystemService方法可以获得一个sensor的实例。永远记得确保当你不需要的时候,特别是Activity暂定的时候,要关闭感应器。忽略这一点肯能导致几个小时就耗尽电池,注意当屏幕关闭时,系统不会自动关闭感应器。

三 常用的感应器

(1) 加速度感应器

可以通过这个感应器获得三个浮点型

x-axis 
y-axis
z-axis

可参阅《android 高级编程2》中的一个插图分析次数据

 Android基于Sensor感应器获取重力感应加速度的方法