The following code shows how to .
Main layout activity_main.xml file
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/direction" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="64dip" android:textStyle="bold" /> <TextView android:id="@+id/values" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> </RelativeLayout>
Main activity Java code
package com.java2s.myapplication3.app; // w w w . ja va2 s . com 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 MainActivity extends Activity implements SensorEventListener { //Allocate data arrays once and reuse float[] temp = new float[9]; float[] rotation = new float[9]; float[] values = new float[3]; private SensorManager mSensorManager; private Sensor mAccelerometer, mField; private TextView valueView, directionView; private float[] mGravity = new float[3]; private float[] mMagnetic = new float[3]; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); valueView = (TextView) findViewById(R.id.values); directionView = (TextView) findViewById(R.id.direction); } protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI); mSensorManager.registerListener(this, mField, SensorManager.SENSOR_DELAY_UI); } protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } private void updateDirection() { SensorManager.getRotationMatrix(temp, null, mGravity, mMagnetic); SensorManager.remapCoordinateSystem(temp, SensorManager.AXIS_X, SensorManager.AXIS_Z, rotation); SensorManager.getOrientation(rotation, values); for (int i = 0; i < values.length; i++) { Double degrees = (values[i] * 180) / Math.PI; values[i] = degrees.floatValue(); } directionView.setText(getDirectionFromDegrees(values[0])); valueView.setText(String.format("Azimuth: %1$1.2f, Pitch: %2$1.2f, Roll: %3$1.2f", values[0], values[1], values[2])); } private String getDirectionFromDegrees(float degrees) { if (degrees >= -22.5 && degrees < 22.5) { return "N"; } if (degrees >= 22.5 && degrees < 67.5) { return "NE"; } if (degrees >= 67.5 && degrees < 112.5) { return "E"; } if (degrees >= 112.5 && degrees < 157.5) { return "SE"; } if (degrees >= 157.5 || degrees < -157.5) { return "S"; } if (degrees >= -157.5 && degrees < -112.5) { return "SW"; } if (degrees >= -112.5 && degrees < -67.5) { return "W"; } if (degrees >= -67.5 && degrees < -22.5) { return "NW"; } return null; } public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: System.arraycopy(event.values, 0, mGravity, 0, event.values.length); break; case Sensor.TYPE_MAGNETIC_FIELD: System.arraycopy(event.values, 0, mMagnetic, 0, event.values.length); break; default: return; } if (mGravity != null && mMagnetic != null) { updateDirection(); } } }