1、关于加速计Accelerometer
这篇文章对传感器做了些讲解。Android手机一般会自带几个传感器,加速计Accelerometer是常见的一种,一些游戏APP就用到了加速计。加速计计算的是三个方向上加速度,单位是加速度基本单位m/s 2。这三个方向分别是水平的两个互相垂直的方向,一个与重力相同的方向。所以我们通过加速计获取的数据就是三个加速度。
水平的两个方向,一般是这样的:把手机放平,其中一个加速度方向是与手机的短边平行的,另一个方向是与手机的长边平行。
在android系统中传感器的运行状态是通过系统服务SensorManager提供的。
笔者的虚拟机中Android系统版本为4.4。
2、如何获取加速计信息
以下代码参考自 http://webtutsdepot.com/2011/08/20/android-sdk-accelerometer-example-tutorial/。
2.1、新建Android项目
2.2、修改布局文件layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="加速计实时数据"
/>
<TextView
android:id="@+id/xcoor"
android:text="X Coordinate: "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/ycoor"
android:text="Y Coordinate: "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/zcoor"
android:text="Z Coordinate: "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
2.3、修改java代码
package com.example.HelloWorld;
import android.app.Activity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;
public class MyActivity extends Activity implements SensorEventListener{
private SensorManager sensorManager;
TextView xCoor; // declare X axis object
TextView yCoor; // declare Y axis object
TextView zCoor; // declare Z axis object
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
xCoor=(TextView)findViewById(R.id.xcoor); // create X axis object
yCoor=(TextView)findViewById(R.id.ycoor); // create Y axis object
zCoor=(TextView)findViewById(R.id.zcoor); // create Z axis object
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
// add listener. The listener will be MyActivity (this) class
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
// 当精度发生变化时调用
public void onAccuracyChanged(Sensor sensor,int accuracy){
}
// 当sensor事件发生时候调用
public void onSensorChanged(SensorEvent event){
// check sensor type
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
// assign directions
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];
xCoor.setText("X: "+x);
yCoor.setText("Y: "+y);
zCoor.setText("Z: "+z);
}
}
@Override
protected void onDestroy() {
sensorManager.unregisterListener(this);
super.onDestroy();
}
}
运行效果如下:

下面对代码略作分析:
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
//传感器精度改变时调用
public abstract void onAccuracyChanged (Sensor sensor, int accuracy)
//传感器获取的数值改变时调用
public abstract void onSensorChanged (SensorEvent event)