Following is an example of how to use the Android accelerometer and orientation API’s. This example is based on Frank Ableson’s “Tapping into Android’s sensors” article over at IBM DeveloperWorks. I’ve made a change to the UI to use the Android TableView and implemented it as a Resource.
To get going, all you need to do is
- Create a new Android project
- Replace the contents of main.xml with the xml in this sample
- Replace the contents of your Activity class with the code in this sample
Following is the new main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:id="@+id/accelerometer_label"
android:layout_column="1"
android:text="Accelerometer"
android:textSize="9pt"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/accel_x_label"
android:layout_column="1"
android:text="X:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_x_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/accel_y_label"
android:layout_column="1"
android:text="Y:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_y_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/accel_z_label"
android:layout_column="1"
android:text="Z:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/accel_z_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView
android:id="@+id/orientation_label"
android:layout_column="1"
android:text="Orientation"
android:textSize="9pt"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/orient_x_label"
android:layout_column="1"
android:text="X:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/orient_x_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/orient_y_label"
android:layout_column="1"
android:text="Y:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/orient_y_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/orient_z_label"
android:layout_column="1"
android:text="Z:"
android:textSize="8pt"
android:padding="3dip" />
<TextView
android:id="@+id/orient_z_value"
android:gravity="right"
android:textSize="8pt"
android:padding="3dip" />
</TableRow>
</TableLayout>
Following is the new Android Activity Java class
package com.blogspot.mobilestrategist;
import com.blogspot.mobilestrategist.R;
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 AccelOrientExample extends Activity implements SensorEventListener {
// Accelerometer X, Y, and Z values
private TextView accelXValue;
private TextView accelYValue;
private TextView accelZValue;
// Orientation X, Y, and Z values
private TextView orientXValue;
private TextView orientYValue;
private TextView orientZValue;
private SensorManager sensorManager = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get a reference to a SensorManager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
// Capture accelerometer related view elements
accelXValue = (TextView) findViewById(R.id.accel_x_value);
accelYValue = (TextView) findViewById(R.id.accel_y_value);
accelZValue = (TextView) findViewById(R.id.accel_z_value);
// Capture orientation related view elements
orientXValue = (TextView) findViewById(R.id.orient_x_value);
orientYValue = (TextView) findViewById(R.id.orient_y_value);
orientZValue = (TextView) findViewById(R.id.orient_z_value);
// Initialize accelerometer related view elements
accelXValue.setText("0.00");
accelYValue.setText("0.00");
accelZValue.setText("0.00");
// Initialize orientation related view elements
orientXValue.setText("0.00");
orientYValue.setText("0.00");
orientZValue.setText("0.00");
}
// This method will update the UI on new sensor events
public void onSensorChanged(SensorEvent sensorEvent) {
synchronized (this) {
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
accelXValue.setText(Float.toString(sensorEvent.values[0]));
accelYValue.setText(Float.toString(sensorEvent.values[1]));
accelZValue.setText(Float.toString(sensorEvent.values[2]));
}
if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
orientXValue.setText(Float.toString(sensorEvent.values[0]));
orientYValue.setText(Float.toString(sensorEvent.values[1]));
orientZValue.setText(Float.toString(sensorEvent.values[2]));
}
}
}
// I've chosen to not implement this method
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
protected void onResume() {
super.onResume();
// Register this class as a listener for the accelerometer sensor
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
// ...and the orientation sensor
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
// Unregister the listener
sensorManager.unregisterListener(this);
super.onStop();
}
}