Android-Position Sensors
来源:互联网 发布:网络歌手音频资料 编辑:程序博客网 时间:2024/05/13 09:47
The Android platform provides two sensors that let you determine the position of a device: the geomagnetic field sensor and the orientation sensor.
Note: The orientation sensor was deprecated in Android 2.2 (API Level 8).
》Using the Game Rotation Vector Sensor
The following code shows you how to get an instance of the default game rotation vector sensor:
private SensorManager mSensorManager;private Sensor mSensor;...mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR);》Using the Geomagnetic Rotation Vector Sensor
The following code shows you how to get an instance of the default geomagnetic rotation vector sensor:
private SensorManager mSensorManager;private Sensor mSensor;...mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR);》Using the Orientation Sensor
The orientation sensor lets you monitor the position of a device relative to the earth's frame of reference (specifically, magnetic north). The following code shows you how to get an instance of the default orientation sensor:
private SensorManager mSensorManager;private Sensor mSensor;...mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
》The following code sample shows how to acquire orientation data directly from the orientation sensor. We recommend that you do this only if a device has negligible roll.
public class SensorActivity extends Activity implements SensorEventListener { private SensorManager mSensorManager; private Sensor mOrientation; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // Do something here if sensor accuracy changes. // You must implement this callback in your code. } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } @Override public void onSensorChanged(SensorEvent event) { float azimuth_angle = event.values[0]; float pitch_angle = event.values[1]; float roll_angle = event.values[2]; // Do something with these orientation angles. }}
》Using the Geomagnetic Field Sensor
The geomagnetic field sensor lets you monitor changes in the earth's magnetic field. The following code shows you how to get an instance of the default geomagnetic field sensor:
private SensorManager mSensorManager;private Sensor mSensor;...mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
》Using the Uncalibrated Magnetometer
private SensorManager mSensorManager;private Sensor mSensor;...mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED);》Using the Proximity Sensor
The proximity sensor lets you determine how far away an object is from a device. The following code shows you how to get an instance of the default proximity sensor:
private SensorManager mSensorManager;private Sensor mSensor;...mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
》
public class SensorActivity extends Activity implements SensorEventListener { private SensorManager mSensorManager; private Sensor mProximity; @Override public final void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Get an instance of the sensor service, and use that to get an instance of // a particular sensor. mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); } @Override public final void onAccuracyChanged(Sensor sensor, int accuracy) { // Do something here if sensor accuracy changes. } @Override public final void onSensorChanged(SensorEvent event) { float distance = event.values[0]; // Do something with this sensor data. } @Override protected void onResume() { // Register a listener for the sensor. super.onResume(); mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { // Be sure to unregister the sensor when the activity pauses. super.onPause(); mSensorManager.unregisterListener(this); }}
Note: Some proximity sensors return binary values that represent "near" or "far." In this case, the sensor usually reports its maximum range value in the far state and a lesser value in the near state. Typically, the far value is a value > 5 cm, but this can vary from sensor to sensor. You can determine a sensor's maximum range by using the getMaximumRange()
method.
- Android-Position Sensors
- Android API Guides---Position Sensors
- Android Sensors
- android-Sensors
- android sensors
- Android Sensors Development
- android Sensors学习笔记
- android sensors整个流程
- Android sensors Hal分析
- android sensors 总结(一)
- android sensors 总结(二)
- android sensors 总结(三)
- android sensors 总结(四)
- android sensors 总结(五)
- android-Motion Sensors
- Android-Environment Sensors
- Android Sensors框架详解
- Android sensors概述
- 制作QT镜像文件
- AIX虚拟内存分页替换的原理及Oracle数据库服务器swap空间满的分析
- Mac电脑安装其他系统
- 51单片机流水灯制作
- Objective-C_语言 练习题2
- Android-Position Sensors
- JAVA实现空间索引编码(GeoHash)
- java设计模式-策略模式
- [Noi 2015] 品酒大会
- 大战!贪心!SHU1951 Apple Trees
- PHP变量的声明及其作用域&静态变量
- ios开发--字典转模型
- ServerSocket服务器通信
- 106,Protocol的基本使用以及注意点