SensorRawAccelerometerActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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.widget.TextView; public class SensorRawAccelerometerActivity extends Activity implements SensorEventListener { private static final int UPDATE_THRESHOLD = 500; private SensorManager mSensorManager; private Sensor mAccelerometer; private TextView mXValueView, mYValueView, mZValueView; private long mLastUpdate; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mXValueView = (TextView) findViewById(R.id.x_value_view); mYValueView = (TextView) findViewById(R.id.y_value_view); mZValueView = (TextView) findViewById(R.id.z_value_view); // Get reference to SensorManager mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); // Get reference to Accelerometer if (null == (mAccelerometer = mSensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER))) finish(); } // Register listener @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI); mLastUpdate = System.currentTimeMillis(); } // Unregister listener @Override protected void onPause() { mSensorManager.unregisterListener(this); super.onPause(); } // Process new reading @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { long actualTime = System.currentTimeMillis(); if (actualTime - mLastUpdate > UPDATE_THRESHOLD) { mLastUpdate = actualTime; float x = event.values[0], y = event.values[1], z = event.values[2]; mXValueView.setText(String.valueOf(x)); mYValueView.setText(String.valueOf(y)); mZValueView.setText(String.valueOf(z)); } } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // N/A } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/x_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:padding="5dp" android:text="@string/raw_x_string" android:textSize="24sp" /> <TextView android:id="@+id/y_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/x_value" android:padding="5dp" android:text="@string/raw_y_string" android:textSize="24sp" /> <TextView android:id="@+id/z_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/y_value" android:padding="5dp" android:text="@string/raw_z_string" android:textSize="24sp" /> <TextView android:id="@+id/x_value_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/x_value" android:padding="5dp" android:textSize="24sp" /> <TextView android:id="@+id/y_value_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/x_value_view" android:layout_toRightOf="@+id/y_value" android:padding="5dp" android:textSize="24sp" /> <TextView android:id="@+id/z_value_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/y_value_view" android:layout_toRightOf="@+id/z_value" android:padding="5dp" android:textSize="24sp" /> </RelativeLayout> |