3434
3535public class TakePicture extends Activity {
3636 private static final int GPS_ENABLE = 2 ;
37+ private static final int TOOK_A_PICTURE = 0 ;
3738 Uri myPicture = null ;
3839 Uri mImageDBUri = null ;
3940 String mImageFilename = "" ;
@@ -71,13 +72,11 @@ public void onCreate(Bundle savedInstanceState) {
7172
7273 locationManager = (LocationManager ) this
7374 .getSystemService (Context .LOCATION_SERVICE );
74-
75- if (initializeGeoLocation ()) {
76- captureImage ();
77- } else {
78- Toast .makeText (getApplicationContext (),
79- "Need the GPS in order to take picture" , Toast .LENGTH_LONG ).show ();
80- finish ();
75+ /*
76+ * only check GPS or take a picture if this is the first run of the activity, ie its saved instance state is null
77+ */
78+ if (savedInstanceState == null ){
79+ initializeGeoLocation ();
8180 }
8281 }
8382
@@ -102,9 +101,8 @@ public void onCaptureImageClick(View view) {
102101 @ Override
103102 protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
104103 switch (requestCode ) {
105- case 0 :
104+ case TOOK_A_PICTURE :
106105
107- if (requestCode == 0 && resultCode == Activity .RESULT_OK ) {
108106 // Now we know that our myPicture URI refers to the image just
109107 // taken
110108 /*
@@ -142,14 +140,43 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
142140 .show ();
143141 }
144142
145- }
143+
146144 finish ();
147145 break ;
148146 case GPS_ENABLE :
149147 if (!locationManager
150148 .isProviderEnabled (android .location .LocationManager .GPS_PROVIDER )) {
149+ Toast .makeText (getApplicationContext (),
150+ "Need the GPS in order to take picture" , Toast .LENGTH_LONG ).show ();
151151 finish ();
152152 }
153+
154+ // Define a listener that responds to location updates
155+ LocationListener locationListener = new LocationListener () {
156+ public void onLocationChanged (Location location ) {
157+ // Called when a new location is found by the network location
158+ // provider.
159+ makeUseOfNewLocation (location );
160+ }
161+
162+ public void onStatusChanged (String provider , int status ,
163+ Bundle extras ) {
164+ }
165+
166+ public void onProviderEnabled (String provider ) {
167+ }
168+
169+ public void onProviderDisabled (String provider ) {
170+ }
171+ };
172+
173+ // Register the listener with the Location Manager to receive location
174+ // updates
175+ locationManager .requestLocationUpdates (
176+ LocationManager .NETWORK_PROVIDER , 0 , 0 , locationListener );
177+
178+ captureImage ();
179+
153180 break ;
154181 default :
155182 break ;
@@ -187,12 +214,12 @@ public String getPath(Uri uri) {
187214 return cursor .getString (column_index );
188215 }
189216
190- private Boolean initializeGeoLocation () {
217+ private void initializeGeoLocation () {
191218 if (!locationManager
192219 .isProviderEnabled (android .location .LocationManager .GPS_PROVIDER )) {
193220 Intent myIntent = new Intent (Settings .ACTION_SECURITY_SETTINGS );
194221 startActivityForResult (myIntent , GPS_ENABLE );
195- }
222+ } else {
196223
197224 // Define a listener that responds to location updates
198225 LocationListener locationListener = new LocationListener () {
@@ -217,7 +244,9 @@ public void onProviderDisabled(String provider) {
217244 // updates
218245 locationManager .requestLocationUpdates (
219246 LocationManager .NETWORK_PROVIDER , 0 , 0 , locationListener );
220- return true ;
247+ captureImage ();
248+ }
249+
221250 }
222251
223252 protected void makeUseOfNewLocation (Location location ) {
0 commit comments