Android系统提供了对传感器的支持。
开发应用传感器很简单,只需要为指定监听器注册一个监听器即可。
步骤:
1.调用Context的getSystemService(Context.SENSOR_SERVICE)方法 获取SensorManager对象,SensorManager对象代表系统的传感器管理服务
2.调用SensorManager的getDefaultSensor(int type)方法来获取指定类型的传感器
3.通常选择在Activity的onResume()方法中调用SensorManager的registerListener()为指定传感去注册监听即可。
程序通过实现监听器即可获取传感器传回来的数据。
SensorManager提供的注册传感器的方法为:registerListener(SensorEventListener listener,Sensor sensor ,int rate ):
参数:
listener:
监听传感器的监听器。该监听器需要实现SensorEventListener接口
sensor:传感对象
rate:指定获取传感器数据的频率。
有如下几个频率:
SensorManager.SENSOR_DELAY_FASTEST:
最快,延迟最小,只有特别依赖于传感器数据的应用推荐采用这种频率。
SensorManager.SENSOR_DELAY_GAME:
适合游戏的频率。在一般实用性的应用上适合使用这种频率。
SensorManager.SENSOR_DELAY_NORMAL:
正常频率。一般实时性要求不是特别高的应用上适合使用。
SensorManager.SENSOR_DELAY_UI:
适合普通用户界面的频率。普通小程序中使用。
Android常用的传感器:
方向、磁场、温度、光、压力。
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
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.sensor.MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/orientation" />
<EditText
android:id="@+id/etOrientation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:editable="false" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/magnetic" />
<EditText
android:id="@+id/etMagnetic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:editable="false" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/temperature" />
<EditText
android:id="@+id/etTemerature"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:editable="false" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/light" />
<EditText
android:id="@+id/etLight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:editable="false" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pressure" />
<EditText
android:id="@+id/etPressure"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:editable="false" />
</LinearLayout>
Activity:
public class MainActivity extends Activity implements SensorEventListener {
// 定义SensorManager
SensorManager mSensorManager;
// 方向
EditText etOrientation;
// 磁场
EditText etMagnetic;
// 温度
EditText etTemerature;
// 光
EditText etLight;
// 压力
EditText etPressure;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etOrientation = (EditText) findViewById(R.id.etOrientation);
etMagnetic = (EditText) findViewById(R.id.etMagnetic);
etTemerature = (EditText) findViewById(R.id.etTemerature);
etLight = (EditText) findViewById(R.id.etLight);
etPressure = (EditText) findViewById(R.id.etPressure);
// 获取传感器管理服务
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
// 为方向传感器注册监听器
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
// 为系统的磁场传感器注册监听器
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_GAME);
// 为系统的温度传感器注册监听器
mSensorManager.registerListener(this, mSensorManager
.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE),
SensorManager.SENSOR_DELAY_GAME);
// 为系统的光传感器注册监听器
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
SensorManager.SENSOR_DELAY_GAME);
// 为系统的压力传感器注册监听器
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(this);
super.onStop();
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
// 以下是实现SensorEventListener接口必须实现的方法
@Override
public void onSensorChanged(SensorEvent event) {
float values[] = event.values;
int sensorType = event.sensor.getType();
StringBuilder sb = new StringBuilder();
switch (sensorType) {
// 方向传感器
case Sensor.TYPE_ORIENTATION:
sb = new StringBuilder();
sb.append("绕Z轴转过的角度:");
sb.append(values[0]);
sb.append("\n绕X轴转过的角度:");
sb.append(values[1]);
sb.append("\n绕Y轴转过的角度:");
sb.append(values[2]);
etOrientation.setText(sb.toString());
break;
// 磁场传感器
case Sensor.TYPE_MAGNETIC_FIELD:
sb = new StringBuilder();
sb.append("X方向上的角度:");
sb.append(values[0]);
sb.append("\nY方向上的角度:");
sb.append(values[1]);
sb.append("\nZ方向上的角度:");
sb.append(values[2]);
etMagnetic.setText(sb.toString());
break;
// 温度传感器
case Sensor.TYPE_AMBIENT_TEMPERATURE:
sb = new StringBuilder();
sb.append("当前温度为:");
sb.append(values[0]);
etTemerature.setText(sb.toString());
break;
// 光传感器
case Sensor.TYPE_LIGHT:
sb = new StringBuilder();
sb.append("当前光的强度为:");
sb.append(values[0]);
etLight.setText(sb.toString());
break;
// 压力传感器
case Sensor.TYPE_PRESSURE:
sb = new StringBuilder();
sb.append("当前压力为:");
sb.append(values[0]);
etPressure.setText(sb.toString());
break;
default:
break;
}
}
// 当传感器精度改变时回调该方法
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
原文地址:http://blog.csdn.net/u013476556/article/details/46609071