source: other-projects/tipple-android/tipple-ar/compass-test/src/comp/namespace/CompassActivity.java@ 26528

Last change on this file since 26528 was 26528, checked in by davidb, 11 years ago

Code developed by the Tipple-AR Smoke and Mirrors team, based on the main tipple trunk

File size: 3.4 KB
Line 
1package comp.namespace;
2
3import android.app.Activity;
4import android.os.Bundle;
5
6import comp.namespace.R;
7
8import android.app.Activity;
9import android.hardware.Sensor;
10import android.hardware.SensorEvent;
11import android.hardware.SensorEventListener;
12import android.hardware.SensorManager;
13import android.os.Bundle;
14import android.widget.TextView;
15
16public class CompassActivity extends Activity implements SensorEventListener {
17
18 // Accelerometer X, Y, and Z values
19 private TextView accelXValue;
20 private TextView accelYValue;
21 private TextView accelZValue;
22
23 // Orientation X, Y, and Z values
24 private TextView orientXValue;
25 private TextView orientYValue;
26 private TextView orientZValue;
27
28 private SensorManager sensorManager = null;
29
30 /** Called when the activity is first created. */
31 @Override
32 public void onCreate(Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
34 // Get a reference to a SensorManager
35 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
36 setContentView(R.layout.main);
37
38 // Capture accelerometer related view elements
39 accelXValue = (TextView) findViewById(R.id.accel_x_value);
40 accelYValue = (TextView) findViewById(R.id.accel_y_value);
41 accelZValue = (TextView) findViewById(R.id.accel_z_value);
42
43 // Capture orientation related view elements
44 orientXValue = (TextView) findViewById(R.id.orient_x_value);
45 orientYValue = (TextView) findViewById(R.id.orient_y_value);
46 orientZValue = (TextView) findViewById(R.id.orient_z_value);
47
48 // Initialise accelerometer related view elements
49 accelXValue.setText("0.00");
50 accelYValue.setText("0.00");
51 accelZValue.setText("0.00");
52
53 // Initialise orientation related view elements
54 orientXValue.setText("0.00");
55 orientYValue.setText("0.00");
56 orientZValue.setText("0.00");
57 }
58
59 // This method will update the UI on new sensor events
60 public void onSensorChanged(SensorEvent sensorEvent) {
61 synchronized (this) {
62 if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
63 accelXValue.setText(Float.toString(sensorEvent.values[0]));
64 accelYValue.setText(Float.toString(sensorEvent.values[1]));
65 accelZValue.setText(Float.toString(sensorEvent.values[2]));
66 }
67
68 if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
69 orientXValue.setText(Float.toString(sensorEvent.values[0]));
70 orientYValue.setText(Float.toString(sensorEvent.values[1]));
71 orientZValue.setText(Float.toString(sensorEvent.values[2]));
72 }
73 }
74 }
75
76 // I've chosen to not implement this method
77 public void onAccuracyChanged(Sensor arg0, int arg1) {
78 // TODO Auto-generated method stub
79
80 }
81
82 @Override
83 protected void onResume() {
84 super.onResume();
85 // Register this class as a listener for the accelerometer sensor
86 sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
87 // ...and the orientation sensor
88 sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
89 }
90
91 @Override
92 protected void onStop() {
93 // Unregister the listener
94 sensorManager.unregisterListener(this);
95 super.onStop();
96 }
97
98}
Note: See TracBrowser for help on using the repository browser.