现在的智能手机都配备了各种各样的传感器,本文将介绍Android SDK提供的传感器开发接口,并通过简单实例展示如何使用这些接口。
android SDK提供的与传感器相关的类有(位于android.hardware包):
Sensor: 表示传感器的类,它保存有传感器名称,厂商,版本,精确度等信息;
SensorEvent:表示传感器事件,它可以保存传感器的值,传感器类型,时间戳等信息;
SensorEventListener:用于接收传感器来自SensorManager的通知,当传感器发生变化时,它包含两个回调函数。
SensorManager:SensorManager让你可以访问设备(手机)的全部传感器。lets you access the device's sensors.
可通过以android.content.Context.SENSOR_SERVICE为参数调用Context.getSystemService()获取一个该类的实例。
注意:应当始终保证在不需要使用传感器的时候禁用传感器,特别是当你的activity暂停的时候。没有这样做将会导致电池只能使用很少几个小时。记住,系统不会在屏幕关闭的时候自动禁用传感器。
SensorListener:已废除,不再介绍。
Android的SensorEvent API定义的坐标系统是:X轴水平向右,Y轴垂直向上,Z轴沿屏幕向外。在这个坐标系统里,屏幕后面的Z值为负。如下图所示:
下面介绍Android Sensor相关API如何使用。
SensorManager的doc里给出了一个简单的demo:
public class SensorActivity extends Activity, implements SensorEventListener { private final SensorManager mSensorManager; private final Sensor mAccelerometer; public SensorActivity() { mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { } }这个demo虽短,但已经给出了一个传感器开发的基本框架,并且在Activity暂停的时禁用了对应的传感器(按照官方的说法可以省电)。
下面是一个传感器测试demo,可以测试你android手机支持那些传感器,并能实时显示传感器的值。
布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.sensortest.MainActivity" > <Button android:id="@+id/btnNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" /> <Button android:id="@+id/btnPause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btnNext" android:layout_alignBottom="@+id/btnNext" android:layout_toRightOf="@+id/btnNext" android:text="Pause" /> <TextView android:id="@+id/txtInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btnNext" android:layout_below="@+id/btnNext" android:layout_marginLeft="15dp" android:layout_marginTop="18dp" android:text="Sensor Informations" /> <TextView android:id="@+id/txtDetails" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/txtInfo" android:layout_below="@+id/txtInfo" android:layout_marginTop="14dp" android:text="Sensor Values" /> </RelativeLayout>
Activity代码如下:
package com.example.sensortest; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private final String TAG = "SensorTest"; SensorManager sensorManager; SensorEventListener listener; List<Sensor> allSensors; volatile int currentIndex = -1; // Next按钮每按一次加一 Button btnNext; Button btnPause; TextView txtInfo; TextView txtValues; AtomicInteger showDetail = new AtomicInteger(1); private void init() { sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); allSensors = sensorManager.getSensorList(Sensor.TYPE_ALL); initViews(); initSensorsListener(); } private void initViews() { btnNext = (Button) findViewById(R.id.btnNext); btnPause = (Button) findViewById(R.id.btnPause); txtInfo = (TextView) findViewById(R.id.txtInfo); txtValues = (TextView) findViewById(R.id.txtDetails); btnNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { currentIndex = (currentIndex + 1) % allSensors.size(); Sensor sensor = allSensors.get(currentIndex); int type = sensor.getType(); txtInfo.setText(String.format("%d: %s, %s", currentIndex + 1, sensorTypeToString(sensor.getType()), sensor.toString())); Log.d(TAG, String.format("%d: %s, %s", currentIndex + 1, sensorTypeToString(sensor.getType()), sensor.toString())); } }); btnPause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDetail.incrementAndGet(); Log.d(TAG, "showDetails: " + showDetail + ", idx: " + currentIndex); if (showDetail.get() % 2 == 1) { btnPause.setText("Pause"); } else { btnPause.setText("Start"); } } }); } private void initSensorsListener() { if (allSensors.size() > 0) { listener = new SensorEventListener() { long lastTime = System.currentTimeMillis(); long time; @Override public void onSensorChanged(SensorEvent event) { time = System.currentTimeMillis(); if (showDetail.get() % 2 == 1) { Sensor sensor = event.sensor; if (currentIndex >= 0 && sensor.getType() == allSensors.get( currentIndex).getType()) { // 只显示currentIndex对应的Sensor数据 StringBuffer str = new StringBuffer(); // for (int i=0; i<event.values.length; i++) { // float value = event.values[i]; for (float value : event.values) { str.append(value + "\n"); } txtValues.setText(str); if (time - lastTime > 1000) { // true || Log.d(TAG, "type: " + sensorTypeToString(event.sensor .getType()) + ", values: " + str.toString() + ", time: " + time); } } } lastTime = time; } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { Log.d(TAG, "onAccuracyChanged sensor: " + sensor + ", accracy: " + accuracy); } }; for (Sensor sensor : allSensors) { sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL); } } } String sensorTypeToString(int type) { String res = null; switch (type) { case Sensor.TYPE_ACCELEROMETER: res = "ACCELEROMETER"; break; case Sensor.TYPE_AMBIENT_TEMPERATURE: res = "AMBIENT_TEMPERATURE"; break; case Sensor.TYPE_GAME_ROTATION_VECTOR: res = "GAME_ROTATION_VECTOR"; break; case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR: res = "GEOMAGNETIC_ROTATION_VECTOR"; break; case Sensor.TYPE_GRAVITY: res = "GRAVITY"; break; case Sensor.TYPE_GYROSCOPE: res = "GYROSCOPE"; break; case Sensor.TYPE_GYROSCOPE_UNCALIBRATED: res = "GYROSCOPE_UNCALIBRATED"; break; case Sensor.TYPE_HEART_RATE: res = "HEART_RATE"; break; case Sensor.TYPE_LIGHT: res = "LIGHT"; break; case Sensor.TYPE_LINEAR_ACCELERATION: res = "LINEAR_ACCELERATION"; break; case Sensor.TYPE_MAGNETIC_FIELD: res = "MAGNETIC_FIELD"; break; case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED: res = "MAGNETIC_FIELD_UNCALIBRATED"; break; case Sensor.TYPE_ORIENTATION: res = "ORIENTATION"; break; case Sensor.TYPE_PRESSURE: res = "PRESSURE"; break; case Sensor.TYPE_PROXIMITY: res = "PROXIMITY"; break; case Sensor.TYPE_RELATIVE_HUMIDITY: res = "HUMIDITY"; break; case Sensor.TYPE_ROTATION_VECTOR: res = "ROTATION_VECTOR"; break; default: res = "UNKNOW"; break; } return res; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } protected void onResume() { super.onResume(); for (Sensor sensor : allSensors) { sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL); } } protected void onPause() { super.onPause(); for (Sensor sensor : allSensors) { sensorManager.unregisterListener(listener, sensor); } } }
实际运行效果图如下: