device027  

小試身手前的小知識

  1. 宣告一個SensorManager,SensorManager管理、調度、處理sensor的工
  2. getSystemService(SENSOR_SERVICE)取得感測器服務
  3. 設定取得的感測器類型 getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
  4. 需要與感測器互動時,必須先注冊藉此監視與感測器相關的活動,方法為SensorManger類別中的registerListener
  5.  利用SensorEventListener監聽Sensor之兩件事, (a) onSensorChanged(SensorEvent event)當感測器的值有所改變。 (b)onAccuracyChanged(Sensor sensor, int accuracy)感測器精準度改變時。
  6.  Sensor更新速度如下表,控制方式為程式碼淺藍色標記部分
  Sensor.manager.SENSOR_DELAY_FASTEST   0ms
  Sensor.manager.SENSOR_DELAY_GAME   20ms
  Sensor.manager.SENSOR_DELAY_UI   60ms
  Sensor.manager.SENSOR_DELAY_NORMAL   200ms

 

小試身手小範例

java檔

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.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class accelerometerDetect extends Activity implements SensorEventListener {
private TextView text_x;
private TextView text_y;
private TextView text_z;
private SensorManager aSensorManager;
private Sensor aSensor;
private float gravity[] = new float[3];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);

text_x = (TextView)findViewById(R.id.TextView01);
text_y = (TextView)findViewById(R.id.TextView02);
text_z = (TextView)findViewById(R.id.TextView03);

aSensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
aSensor = aSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
aSensorManager.registerListener(this, aSensor, aSensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
gravity[0] = event.values[0];
gravity[1] = event.values[1];
gravity[2] = event.values[2];
text_x.setText("X = "+gravity[0]);
text_y.setText("Y = "+gravity[1]);
text_z.setText("Z = "+gravity[2]);
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
/* 取消註冊SensorEventListener */
aSensorManager.unregisterListener(this);
Toast.makeText(this, "Unregister accelerometerListener", Toast.LENGTH_LONG).show();
super.onPause();
}
}

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="三軸加速度 - 資料擷取"
android:textSize="25sp"
android:layout_margin="15sp"
android:layout_marginBottom="10sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X"
android:id="@+id/TextView01"
android:textSize="25sp"
android:layout_margin="15sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y"
android:id="@+id/TextView02"
android:textSize="25sp"
android:layout_margin="15sp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z"
android:id="@+id/TextView03"
android:textSize="25sp"
android:layout_margin="15sp"
/>
</LinearLayout>

 

文章標籤

創作者介紹
創作者 S 的頭像
S

S's Journal

S 發表在 痞客邦 留言(2) 人氣()