现在每部Android 手机里面都会内置有许多的传感器,它们能够监测到各种发生在手机上的物理事件,而我们只要灵活运用这些事件就可以编写出很多好玩的应用程序。今天我们开始简单的传感器使用的学习。
目录导航:
从Android1.5开始,系统内置了对多达八种传感器的支持,他们分别是:加速度传感器(accelerometer)、陀螺仪(gyroscope)、环境光照传感器(light)、磁力传感器(magnetic field)、方向传感器(orientation)、压力传感器(pressure)、距离传感器(proximity)和温度传感器(temperature)。
String service_name = Context.SENSOR_SERVICE;
SensorManager sensorManager = (SensorManager)getSystemService(service_name);
从传感器管理器中获取其中某个或者某些传感器的方法有如下三种:
第一种:获取某种传感器的默认传感器:
Sensor defaultGyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
第二种:获取某种传感器的列表:
List
第三种:获取所有传感器的列表:
List
方法
描述
getMaximumRange()
最大取值范围
getName()
设备名称
getPower()
功率
getResolution()
精度
getType()
传感器类型
getVentor()
设备供应商
getVersion()
设备版本号
传感器类型常量
内部整数值
中文名称
Sensor.TYPE_ACCELEROMETER
1
加速度传感器
Sensor.TYPE_MAGNETIC_FIELD
2
磁力传感器
Sensor.TYPE_ORIENTATION
3
方向传感器
Sensor.TYPE_GYROSCOPE
4
陀螺仪传感器
Sensor.TYPE_LIGHT
5
环境光照传感器
Sensor.TYPE_PRESSURE
6
压力传感器
Sensor.TYPE_TEMPERATURE
7
温度传感器
Sensor.TYPE_PROXIMITY
8
距离传感器
我们创建一个传感器的应用实例,项目结构如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAT4AAADXCAIAAAAMZqqZAAASLklEQVR4nO2dz28c1QHH56/orRcOPVWsemjVkdoKcanUQ09WJW4VKnNoTxF4FRIRQykitGU4VIohoSVQSkB0f4S1x2QxibEtgmsnxtnBDmvHKbHXPhBw2HgjbSVre5hf781782NnZ3bf2/1+NFJ2Z9+P2dgfvzcz+76rnDt3rgMAkA0F6gIgI1AXACmBugBICdQFQEqgLgBSkpq6H5953LdtL19MpWUAAEu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fsSK0eG1zCPA4Om3KdRLbilqfE1qDUAukNEdfsVK5exuvzAOqgL0kEsdfsbK9eFunTcOhsSyXvMD6wLag2A7hBF3UHEynUz6mrsdaXICTM3sC6oNQC6QxR1E5P9fd1057SYIYN0kF7d7IG6QESgbiRQF4gI1AVASqAuAFICdQGQEqgLgJRAXQCkBOoCICVQFwApkV5dxMqB0WQI1U3VXvcbtAEQC1HUFS5WzgbqAkERRV1RY+WgLhAUsdQVL1YO6gJBEU7dvsTKUSHL3hNOCpyjLrlqgFpB4AXEUQ1iOS7IGEHVzTxWzlsbTwTJcVLgwtUlV9g7e7E0CPQFQdXtX6wcbRozYIaq64245MBr7cU3J4BsEVHdvsTK2e7WdZUYfdkUuCh1Aw21/ghAYJAVYqnbx1i5Tqeuq6qmOeYGpMCR6pKOe1HMxFmvXVLnnRMDkC6iqNv3WLkOk8fITYHzrjC7LxOv8i9J4asNQB8QRd3EZB8rB4CISK8uAKMJ1AVASqAuAFICdQGQEqgLgJRAXQCkBOoCICVQFwAp+T/GWP++tzp9yAAAAABJRU5ErkJggg==" alt="" />
private final static String TAG = "SensorTest";
private TextView textView;
SensorManager sm = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
}
// 显示手机支持的传感器的清单
public void allSensors(View view) {
//从系统服务中获得传感器管理器
//从传感器管理器中获得全部的传感器列表
List
Log.i(TAG, "allsensors: " + allSensors.size());
//显示每个传感器的具体信息
for (Sensor sensor : allSensors) {
String name = sensor.getName();
String vendor = sensor.getVendor();
int version = sensor.getVersion();
Log.i(TAG, "name: " + name + ", vendor: " + vendor + ", version: " + version);
}
}
// 光照传感器的使用
public void lightSensors(View view) {
Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
sm.registerListener(LightListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
// 光照传感器的事件
private SensorEventListener LightListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float value = event.values[0];
Sensor sensor = event.sensor;
textView.setText(value + "");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.i(TAG, "on accuracy change: " + accuracy);
}
};
// 加速度传感器
public void accelerSensors(View view) {
Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(AcclerListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
// 加速度传感器的事件
private SensorEventListener AcclerListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// 加速度可能会是负值,所以要取它们的绝对值
float xValue = Math.abs(event.values[0]);
float yValue = Math.abs(event.values[1]);
float zValue = Math.abs(event.values[2]);
if (xValue > 15 || yValue > 15 || zValue > 15) {
// 认为用户摇动了手机,触发摇一摇逻辑
Toast.makeText(MainActivity.this, "摇一摇", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.i(TAG, "on accuracy change: " + accuracy);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (sm != null) {
sm.unregisterListener(LightListener);
sm.unregisterListener(AcclerListener);
}
}
allsensors: 27
name: Accelerometer, vendor: STMicroelectronics, version: 1
name: Magnetometer, vendor: AKM, version: 1
name: Magnetometer Uncalibrated, vendor: AKM, version: 1
name: Gyroscope, vendor: STMicroelectronics, version: 1
name: Gyroscope Uncalibrated, vendor: STMicroelectronics, version: 1
name: Proximity Sensor, vendor: TAOS, version: 1
name: Ambient Light Sensor, vendor: TAOS, version: 1
name: Barometer Sensor, vendor: BOSCH, version: 1
name: Temperature Sensor, vendor: BOSCH, version: 1
name: Gravity, vendor: Qualcomm, version: 1
name: Linear Acceleration, vendor: Qualcomm, version: 1
name: Rotation Vector, vendor: Qualcomm, version: 1
name: Step Detector, vendor: Qualcomm, version: 1
name: Step Counter, vendor: Qualcomm, version: 1
name: Significant Motion Detector, vendor: Qualcomm, version: 1
name: Game Rotation Vector, vendor: Qualcomm, version: 1
name: Geomagnetic Rotation Vector, vendor: Qualcomm, version: 1
name: Orientation, vendor: Qualcomm, version: 1
name: AMD, vendor: Qualcomm, version: 1
name: RMD, vendor: Qualcomm, version: 1
name: Basic Gestures, vendor: Qualcomm, version: 1
name: Facing, vendor: Qualcomm, version: 1
name: Tilt, vendor: Qualcomm, version: 1
name: Pedometer, vendor: Qualcomm, version: 1
name: PEDESTRIAN-ACTIVITY-MONITOR, vendor: Qualcomm, version: 1
name: Motion Accel, vendor: Qualcomm, version: 1
name: Temperature Sensor, vendor: BOSCH, version: 1
手机扫一扫
移动阅读更方便
你可能感兴趣的文章