package com.exercise.AndroidCompass; import java.util.List; import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.widget.Toast; public class AndroidCompass extends Activity { private static SensorManager mySensorManager; private boolean sersorrunning; private MyCompassView myCompassView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myCompassView = (MyCompassView)findViewById(R.id.mycompassview); mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION); if(mySensors.size() > 0){ mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL); sersorrunning = true; Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show(); sersorrunning = false; finish(); } } private SensorEventListener mySensorEventListener = new SensorEventListener(){ @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {
Source: http://android-er.blogspot.com/2010/08/simple-compass-sensormanager-and.html
http://developer.android.com/reference/android/hardware/SensorManager.html
It should be what you are looking for. The OnSensorChanged method should provide the same functionality. (float)event.values[0] - your title.
Just read your thanks text. This is the only way to do this, from everything I have researched, and from my experience in general. For example, I brought you what you need pretty quickly, with copy and paste.
source share