2010年10月10日星期日

檢測加速度計(Accelerometer)的詳細數據, SensorEventListener()

前文談到如何使用getSensorList()方法獲取系統傳感器的信息, 現在以加速度計(Accelerometer)作為一個例子, 進一步檢測某一傳感器的數據.

Accelerometer: 加速規,又稱加速計、加速針、加速度传感器、重力加速度传感器等等,是測量加速度的裝置。相對於遠距感測的裝置,它測量的是自身的運動 ~ (ref: 加速規 - 维基百科)

檢測加速度計(Accelerometer)的詳細數據

首先, 調用getSensorList(Sensor.TYPE_ACCELEROMETER), 檢查返回列表的長度, 以確保加速度計存在.

如果存在,在onResume()註冊一個事件監聽器(accelerometerListener())為SensorEventListener. 並在onStop()註銷.

在SensorEventListener()的onSensorChanged事件傳回的是一個傳感器事件對象(SensorEvent), 其中SensorEvent.value[0], SensorEvent.value[1]和SensorEvent.value[2]就是x, y, z值, 測量接觸力, 以SI(m/s^2)為單位.
- 當設備從左邊移向右邊時, X為負加速度值.
- 當設備平放在一張桌子,加速度值是-STANDARD_GRAVITY.

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="@string/hello"
/>
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textx"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X: "
/>
<TextView
android:id="@+id/texty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y: "
/>
<TextView
android:id="@+id/textz"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z: "
/>
</LinearLayout>


AndroidAccelerometer.java
package com.AndroidAccelerometer;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidAccelerometer extends Activity {

SensorManager sensorManager;
boolean accelerometerPresent;
Sensor accelerometerSensor;

TextView textInfo, textX, textY, textZ;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textInfo = (TextView)findViewById(R.id.info);
textX = (TextView)findViewById(R.id.textx);
textY = (TextView)findViewById(R.id.texty);
textZ = (TextView)findViewById(R.id.textz);

sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);

if(sensorList.size() > 0){
accelerometerPresent = true;
accelerometerSensor = sensorList.get(0);

String strSensor = "Name: " + accelerometerSensor.getName()
+ "\nVersion: " + String.valueOf(accelerometerSensor.getVersion())
+ "\nVendor: " + accelerometerSensor.getVendor()
+ "\nType: " + String.valueOf(accelerometerSensor.getType())
+ "\nMax: " + String.valueOf(accelerometerSensor.getMaximumRange())
+ "\nResolution: " + String.valueOf(accelerometerSensor.getResolution())
+ "\nPower: " + String.valueOf(accelerometerSensor.getPower())
+ "\nClass: " + accelerometerSensor.getClass().toString();
textInfo.setText(strSensor);
}
else{
accelerometerPresent = false;
}

}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

if(accelerometerPresent){
sensorManager.registerListener(accelerometerListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
Toast.makeText(this, "Register accelerometerListener", Toast.LENGTH_LONG).show();
}
}

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();

if(accelerometerPresent){
sensorManager.unregisterListener(accelerometerListener);
Toast.makeText(this, "Unregister accelerometerListener", Toast.LENGTH_LONG).show();
}
}

private SensorEventListener accelerometerListener = new SensorEventListener(){

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
textX.setText("X: " + String.valueOf(event.values[0]));
textY.setText("Y: " + String.valueOf(event.values[1]));
textZ.setText("Z: " + String.valueOf(event.values[2]));
}};
}


相關文章: 檢測Android的方向(Orientation) - Azimuth, Pitch, Roll

沒有留言:

發佈留言