Compass sensor
package app.test;
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 Test extends Activity implements SensorEventListener {
private SensorManager mgr;
private Sensor compass;
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
compass = mgr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
text = (TextView) findViewById(R.id.text);
}
@Override
protected void onResume() {
mgr.registerListener(this, compass, SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
}
@Override
protected void onPause() {
mgr.unregisterListener(this, compass);
super.onPause();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
String msg = String.format("X: %8.4f\nY: %8.4f\nZ: %8.4f",
event.values[0], event.values[1], event.values[2]);
text.setText(msg);
text.invalidate();
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TextView android:id="@+id/text" android:textSize="20sp"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
Related examples in the same category