Activity程式碼如下:
package com.example.acc;
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.WindowManager;
import android.widget.TextView;
public class Acc extends Activity implements SensorEventListener {
private TextView[] textViews = new TextView[3];
private SensorManager manager;
private Sensor sensor;
private final float FILTERING_VALAUE = 0.1f;
private float lowX, lowY, lowZ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acc);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
init();
}
private void init() {
manager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
textViews[0] = (TextView) findViewById(R.id.textView1);
textViews[1] = (TextView) findViewById(R.id.textView2);
textViews[2] = (TextView) findViewById(R.id.textView3);
}
@Override
protected void onPause() {
manager.unregisterListener(this);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
manager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float x = event.values[manager.DATA_X];
float y = event.values[manager.DATA_Y];
float z = event.values[manager.DATA_Z];
lowX = x * FILTERING_VALAUE + lowX * (1.0f - FILTERING_VALAUE);
lowY = y * FILTERING_VALAUE + lowY * (1.0f - FILTERING_VALAUE);
lowZ = z * FILTERING_VALAUE + lowZ * (1.0f - FILTERING_VALAUE);
float highX = x - lowX;
float highY = y - lowY;
float highZ = z - lowZ;
textViews[0].setText(String.valueOf(highX));
textViews[1].setText(String.valueOf(highY));
textViews[2].setText(String.valueOf(highZ));
Log.i("X", "" + x);
Log.i("Y", "" + y);
Log.i("Z", "" + z);
}
}
}
Layout.xml如下:
<LinearLayout 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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
參考文章:
http://android.tgbus.com/Android/tutorial/201106/357188.shtml