1 | package comp.namespace;
|
---|
2 |
|
---|
3 | import android.app.Activity;
|
---|
4 | import android.os.Bundle;
|
---|
5 |
|
---|
6 | import comp.namespace.R;
|
---|
7 |
|
---|
8 | import android.app.Activity;
|
---|
9 | import android.hardware.Sensor;
|
---|
10 | import android.hardware.SensorEvent;
|
---|
11 | import android.hardware.SensorEventListener;
|
---|
12 | import android.hardware.SensorManager;
|
---|
13 | import android.os.Bundle;
|
---|
14 | import android.widget.TextView;
|
---|
15 |
|
---|
16 | public 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 | } |
---|