转载自:http://www.open-open.com/lib/view/open1366172800984.html
下面是Android 定义的代表sensor的几个常量:
int | TYPE_ACCELEROMETER | A constant describing an accelerometer sensor type.:加速传感器,单位是m/s²,测量应用于设备X,Y,Z轴上的加速度,又叫做G-sensor。 |
int | TYPE_ALL | A constant describing all sensor types. |
int | TYPE_AMBIENT_TEMPERATURE | A constant describing an ambient temperature sensor type.:温度传感器,单位是℃,测量返回当前的温度。 |
int | TYPE_GAME_ROTATION_VECTOR | A constant describing an uncalibrated rotation vector sensor type. |
int | TYPE_GEOMAGNETIC_ROTATION_VECTOR | A constant describing the geo-magnetic rotation vector. |
int | TYPE_GRAVITY | A constant describing a gravity sensor type.:重力传感器,单位是m/s²,测量应用于设备X,Y,Z轴上的重力,也叫GV-sensor,地球上的值是9.8m/s²。 |
int | TYPE_GYROSCOPE | A constant describing a gyroscope sensor type.:陀螺仪传感器,单位是rad/s,测量设备X,Y,Z三轴的角加速度数据。 |
int | TYPE_GYROSCOPE_UNCALIBRATED | A constant describing an uncalibrated gyroscope sensor type. |
int | TYPE_LIGHT | A constant describing a light sensor type.:光线感应传感器,单位是lx,检测周围的光线强度,手机系统中主要用于调节LCD亮度。 |
int | TYPE_LINEAR_ACCELERATION | A constant describing a linear acceleration sensor type.:线性加速度传感器,单位是m/s²,该传感器是获取加速度传感器去除重力的影响得到的数据。 |
int | TYPE_MAGNETIC_FIELD | A constant describing a magnetic field sensor type.:磁力传感器,单位是uT(微特斯拉),测量设备周围三个物理轴(x,y,z)的磁场。 |
int | TYPE_MAGNETIC_FIELD_UNCALIBRATED | A constant describing an uncalibrated magnetic field sensor type. |
int | TYPE_ORIENTATION | This constant was deprecated in API level 8. use SensorManager.getOrientation() instead.:方向传感器,测量设备周围三个物理轴(x,y,z)的旋转角度。 |
int | TYPE_PRESSURE | A constant describing a pressure sensor type.:压力传感器,单位是hPa(百帕斯卡),返回当前环境下的压强。 |
int | TYPE_PROXIMITY | A constant describing a proximity sensor type.:距离传感器,单位是cm,用来测量某个对象到屏幕的距离,可用于打电话时判断人耳到电话屏幕距离来进行关闭屏幕的省电功能。 |
int | TYPE_RELATIVE_HUMIDITY | A constant describing a relative humidity sensor type.:湿度传感器,单位是%,来测量周围环境的相对湿度。 |
int | TYPE_ROTATION_VECTOR | A constant describing a rotation vector sensor type.:旋转矢量传感器,旋转矢量代表设备的方向,是一个将坐标轴和角度混合计算得到的数据。 |
int | TYPE_SIGNIFICANT_MOTION | A constant describing a significant motion trigger sensor. |
int | TYPE_STEP_COUNTER | A constant describing a step counter sensor. |
int | TYPE_STEP_DETECTOR | A constant describing a step detector sensor. |
int | TYPE_TEMPERATURE | This constant was deprecated in API level 14. use Sensor.TYPE_AMBIENT_TEMPERATURE instead. |
<span style="font-family:Comic Sans MS;font-size:18px;">通过下面的方法自己检测一下手中的设备都包含哪些传感器:</span>
package com.example.demo;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textview);
tv.setMovementMethod(new ScrollingMovementMethod());
SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//从系统服务中获得传感器管理器
List<Sensor> allSensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
//从传感器管理器中获得全部的传感器列表
tv.setText("经检测该设备有" + allSensors.size() + "个传感器,他们分别是:\n\n");
//显示有多少个传感器
for(Sensor s : allSensors){//显示每个传感器的具体信息
String tempString = "\n" + " 设备名称:" +s.getName() + "\n"
+ " 设备版本:" + s.getVersion() + "\n" + " 供应商:"
+ s.getVendor() + "\n";
switch (s.getType()) {
case Sensor.TYPE_ACCELEROMETER:
tv.setText(tv.getText().toString() + s.getType()
+ " 加速度传感器accelerometer" + tempString + "\n");
break;
case Sensor.TYPE_AMBIENT_TEMPERATURE:
tv.setText(tv.getText().toString() + s.getType()
+ " 温度传感器temperature" + tempString + "\n");
break;
case Sensor.TYPE_GRAVITY:
tv.setText(tv.getText().toString() + s.getType()
+ " 重力传感器gravity" + tempString + "\n");
break;
case Sensor.TYPE_GYROSCOPE:
tv.setText(tv.getText().toString() + s.getType()
+ " 陀螺仪传感器gyroscope" + tempString + "\n");
break;
case Sensor.TYPE_LIGHT:
tv.setText(tv.getText().toString() + s.getType()
+ " 环境光线传感器light" + tempString + "\n");
break;
case Sensor.TYPE_LINEAR_ACCELERATION:
tv.setText(tv.getText().toString() + s.getType()
+ " 线性加速度传感器linear_accelerometer" + tempString + "\n");
break;
case Sensor.TYPE_MAGNETIC_FIELD:
tv.setText(tv.getText().toString() + s.getType()
+ " 电磁场传感器magnetic field" + tempString + "\n");
break;
case Sensor.TYPE_ORIENTATION:
tv.setText(tv.getText().toString() + s.getType()
+ " 方向传感器orientation" + tempString + "\n");
break;
case Sensor.TYPE_PRESSURE:
tv.setText(tv.getText().toString() + s.getType()
+ " 压力传感器pressure" + tempString + "\n");
break;
case Sensor.TYPE_PROXIMITY:
tv.setText(tv.getText().toString() + s.getType()
+ " 距离传感器proximity" + tempString + "\n");
break;
case Sensor.TYPE_RELATIVE_HUMIDITY:
tv.setText(tv.getText().toString() + s.getType()
+ " 湿度传感器relative_humidity" + tempString + "\n");
break;
case Sensor.TYPE_ROTATION_VECTOR:
tv.setText(tv.getText().toString() + s.getType()
+ " 旋转矢量传感器rotation_vector" + tempString + "\n");
break;
case Sensor.TYPE_TEMPERATURE:
tv.setText(tv.getText().toString() + s.getType()
+ " 温度传感器temperature" + tempString + "\n");
break;
default:
tv.setText(tv.getText().toString() + s.getType() + " 未知传感器"
+ tempString);
break;
}
}
}
}
该程序在魅族 MX2上的运行结果如下:
代码:http://download.youkuaiyun.com/detail/yegucheng2618/7621363