/[projects]/android/FriendRadar/src/dk/thoerup/friendradar/RadarActivity.java
ViewVC logotype

Contents of /android/FriendRadar/src/dk/thoerup/friendradar/RadarActivity.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 264 - (show annotations) (download)
Thu Aug 13 19:03:06 2009 UTC (14 years, 9 months ago) by torben
File size: 3341 byte(s)
First version
1 package dk.thoerup.friendradar;
2
3 import java.util.List;
4
5 import android.app.Activity;
6 import android.content.Context;
7 import android.hardware.Sensor;
8 import android.hardware.SensorEvent;
9 import android.hardware.SensorEventListener;
10 import android.hardware.SensorManager;
11 import android.location.Location;
12 import android.location.LocationListener;
13 import android.location.LocationManager;
14 import android.net.UrlQuerySanitizer.ValueSanitizer;
15 import android.os.Bundle;
16 import android.util.Log;
17
18 public class RadarActivity extends Activity implements LocationListener {
19
20 LocationManager locManager;
21 SensorManager sensorManager;
22
23 RadarView radar;
24 @Override
25 public void onCreate(Bundle savedInstanceState) {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.main);
28 radar = (RadarView) findViewById(R.id.radar);
29
30 locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
31 sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
32 }
33
34
35
36
37 @Override
38 protected void onResume() {
39 super.onResume();
40
41 if (locManager.isProviderEnabled("gps")) {
42 locManager.requestLocationUpdates("gps", 0, 0, this);
43 } else {
44 // ToDo: alert the user about missing gps provider
45 }
46
47 List<Sensor> orientationSensors = sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
48 Log.e("sensors1", "sensor count" + orientationSensors.size());
49 if (orientationSensors.size() > 0) {
50 sensorManager.registerListener(orientationListener, orientationSensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
51 } else {
52 // ToDo: alert the user about missing orientation sensor
53 Log.e("sensor", "no orientation sensor");
54 }
55
56 List<Sensor> magneticSensors = sensorManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);
57 Log.e("sensors2", "sensor count" + magneticSensors.size());
58 if (magneticSensors.size() > 0) {
59 sensorManager.registerListener(magneticListener, magneticSensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
60 } else {
61 // ToDo: alert the user about missing orientation sensor
62 Log.e("sensor", "no magnetic sensor");
63 }
64 }
65
66
67
68 @Override
69 protected void onPause() {
70 super.onPause();
71
72 locManager.removeUpdates(this);
73 sensorManager.unregisterListener(orientationListener);
74 sensorManager.unregisterListener(magneticListener);
75 }
76
77
78
79
80 @Override
81 public void onLocationChanged(Location location) {
82 radar.setCurrentLocation(location);
83 }
84
85
86
87
88 @Override
89 public void onProviderDisabled(String provider) {
90 }
91
92
93
94
95 @Override
96 public void onProviderEnabled(String provider) {
97 }
98
99
100
101
102 @Override
103 public void onStatusChanged(String provider, int status, Bundle extras) {
104 }
105
106 SensorEventListener orientationListener = new SensorEventListener() {
107
108 @Override
109 public void onSensorChanged(SensorEvent event) {
110 radar.setHeading( (int)event.values[0]);
111 }
112
113 @Override
114 public void onAccuracyChanged(Sensor sensor, int accuracy) {
115 }
116 };
117
118 SensorEventListener magneticListener = new SensorEventListener() {
119
120 @Override
121 public void onSensorChanged(SensorEvent event) {
122 Log.e("Mag0", "" + event.values[0]);
123 Log.e("Mag1", "" + event.values[1]);
124 Log.e("Mag2", "" + event.values[2]);
125 }
126
127 @Override
128 public void onAccuracyChanged(Sensor sensor, int accuracy) {
129 }
130 };
131
132
133 }

  ViewVC Help
Powered by ViewVC 1.1.20