Woe - I'm still struggling to write some code that can reliably get me
an accurate location fix.

I have written some code that I thought would (i) register with any
available location listener (ii) at onLocationChanged events, check
the location fixes for age and accuracy (iii) once a suitable location
fix was obtained, start an upload, and unregister the location
listeners.

However, it seems to get into a situation sometimes where
onLocationChanged isn't called at all. Yet in my log files, I can
still see the Android NetworkLocationProvider logging
onCellLocationChanged events. Is this a bug in my code?

The code and logs are below. Many thanks for your help.

        LocationManager locationmanager = null;
        LocationListener[] listener;
        Location location;
        private Double latitude, longitude;
        private String latString = null, lonString = null;
        long firstGPSFixTime = 0, latestGPSFixTime = 0;
        int locAccuracy;
        long locationTimeStored = 0;
        public Boolean locationDetermined = false;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                Log.d(LOG_TAG, "onCreate");
                super.onCreate(savedInstanceState);
                setContentView(R.layout.interstitial);
                extras = getIntent().getExtras();
                name = extras.getString("name");
                //Check whether the upload is under way already - we don't want
things to upload multiple times...
                if (savedInstanceState == null) {
                        upload_under_way = false;
                } else {
                        upload_under_way =
savedInstanceState.getBoolean("upload_under_way");
                }
                // if it's not, attempt to get a location fix, and
show a notification
                if (!upload_under_way) {
                        testProviders();
                        mNotificationManager =
                                
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                        showNotification();
                }
        }

        //
**************************************************************************
        // Register with any available provider, and start checking updates
        //
**************************************************************************

        public boolean testProviders() {
                Log.e(LOG_TAG, "testProviders");
                String location_context = Context.LOCATION_SERVICE;
                locationmanager = (LocationManager)
getSystemService(location_context);
                Location lastLocation = null;
                providers = locationmanager.getProviders(true);
                listener = new LocationListener[10]; //horrible code
                int i = 0;
                //register with any available provider, and start
checking updates
                for (String provider : providers) {
                        Log.e(LOG_TAG, "registering with provider " + provider);
                        listener[i] = new LocationListener() {
                                public void onLocationChanged(Location 
location) {
                                        Log.e(LOG_TAG, "onLocationChanged");
                                        if (!locationDetermined) {
                                                checkLoc(location);
                                        }
                                }
                                public void onProviderDisabled(String provider) 
{
                                }
                                public void onProviderEnabled(String provider) {
                                        Log.e(LOG_TAG, "onProviderEnabled");
                                }
                                public void onStatusChanged(String provider, 
int status,
                                                Bundle extras) {
                                }
                        };
                        locationmanager.requestLocationUpdates(provider, 0,
                                        0, listener[i]);
                        lastLocation = 
locationmanager.getLastKnownLocation(provider);
                        i++;
                }
                Log.e(LOG_TAG, "getting updates");
                if (lastLocation != null) {
                        Log.e(LOG_TAG, "getLastKnownLocation is NOT null");
                        checkLoc(lastLocation);
                } else {
                        Log.e(LOG_TAG, "getLastKnownLocation is null");
                }
                return true;
        }

        // function called in onLocationChanged: check accuracy and
age of location fix
        // if it passes the test, start upload, and unregister the
location listeners - else just return false
        public boolean checkLoc(Location location) {
                Log.d(LOG_TAG, "checkLocation");
                float tempAccuracy = location.getAccuracy();
                locAccuracy = (int) tempAccuracy;
                latestGPSFixTime = location.getTime();
                if (firstGPSFixTime == 0) {
                        firstGPSFixTime = latestGPSFixTime;
                }
                float timeDiffSecs = (latestGPSFixTime - firstGPSFixTime) / 
1000;
                Log.e(LOG_TAG, "checkLocation, accuracy = " + locAccuracy
                                + ", firstGPSFixTime = " + firstGPSFixTime + ", 
gpsTime = "
                                + latestGPSFixTime + ", timeDiffSecs = " +
Float.toString(timeDiffSecs));
                // Check our location - search for a maximum of 60 seconds
                if ((locAccuracy > 200) || (timeDiffSecs == 0) || (locAccuracy 
== 0)
&& (timeDiffSecs < 60.0)) {
                        Log.e(LOG_TAG, "Requirements not met yet");
                } else {
                        // if all the requirements have been met, proceed
                        Log.e(LOG_TAG, "Requirements met!!");
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        latString = latitude.toString();
                        lonString = longitude.toString();
                        locationDetermined = true;
                        mNotificationManager.cancel(SIMPLE_NOTIFICATION_ID);
                        Intent i = new Intent(Interstitial.this, 
UploadService.class);
                        i.putExtra("name", name);
                        i.putExtra("latString", latString);
                        i.putExtra("lonString", lonString);
                        if (!upload_under_way) {
                                startService(i);
                                upload_under_way = true;
                                Log.e(LOG_TAG, "set upload_under_way to " + 
upload_under_way);
                        }
                        //get rid of the location listeners...
                        removeListeners();
                }
                return false;
        }

----- Log output on an occasion when the code seems to forget that
'onLocationChanged' exists at all. We register with the 'network'
provider, get an initial fix (which the code above always discards, in
case it's days out of date), and then nothing happens at all, even
though 'onCellLocationChanged' events are still being logged. ------

04-15 16:06:55.148: INFO/ActivityManager(74): Starting activity:
Intent { cmp=com.election2010photo/.Interstitial (has extras) }
04-15 16:06:55.188: DEBUG/Interstitial(2477): onCreate
04-15 16:06:55.198: ERROR/Interstitial(2477): testProviders
04-15 16:06:55.209: DEBUG/LocationManager(2477): Constructor: service
= android.location.ilocationmanager$stub$pr...@449a9e60
04-15 16:06:55.209: ERROR/Interstitial(2477): registering with
provider network
04-15 16:06:55.218: DEBUG/NetworkLocationProvider(74): addListener():
com.election2010photo
04-15 16:06:55.218: DEBUG/NetworkLocationProvider(74): setMinTime: 0
04-15 16:06:55.218: ERROR/Interstitial(2477): getting updates
04-15 16:06:55.218: ERROR/Interstitial(2477): getLastKnownLocation is
NOT null
04-15 16:06:55.218: DEBUG/Interstitial(2477): checkLocation
04-15 16:06:55.218: ERROR/Interstitial(2477): upload_under way = false
04-15 16:06:55.218: ERROR/Interstitial(2477): checkLocation, accuracy
= 2566, firstGPSFixTime = 1271343516689, gpsTime = 1271343516689,
timeDiffSecs = 0.0
04-15 16:06:55.218: ERROR/Interstitial(2477): Requirements not met yet
04-15 16:06:55.218: DEBUG/Interstitial(2477): showNotification
04-15 16:06:55.248: DEBUG/NetworkLocationProvider(74):
onCellLocationChanged [892,8580]
04-15 16:06:55.598: DEBUG/dalvikvm(74): GC freed 7543 objects / 384216
bytes in 95ms
04-15 16:06:55.648: INFO/ActivityManager(74): Displayed activity
com.election2010photo/.Interstitial: 485 ms (total 485 ms)
04-15 16:07:08.178: DEBUG/dalvikvm(2380): GC freed 633 objects / 50040
bytes in 102ms
04-15 16:07:13.208: DEBUG/dalvikvm(148): GC freed 10263 objects /
779328 bytes in 135ms
04-15 16:07:20.138: DEBUG/dalvikvm(2101): GC freed 1065 objects /
814528 bytes in 61ms
04-15 16:07:26.178: DEBUG/dalvikvm(2286): GC freed 3134 objects /
220184 bytes in 100ms
04-15 16:07:31.168: DEBUG/dalvikvm(2114): GC freed 127 objects / 4704
bytes in 87ms
04-15 16:07:45.278: DEBUG/NetworkLocationProvider(74):
onCellLocationChanged [892,8580]
04-15 16:08:35.308: DEBUG/NetworkLocationProvider(74):
onCellLocationChanged [892,8580]
04-15 16:09:25.338: DEBUG/NetworkLocationProvider(74):
onCellLocationChanged [892,8580]
04-15 16:09:35.468: DEBUG/dalvikvm(1767): GC freed 44 objects / 1672
bytes in 100ms
04-15 16:10:15.389: DEBUG/NetworkLocationProvider(74):
onCellLocationChanged [892,8580]
04-15 16:11:05.498: DEBUG/dalvikvm(134): GC freed 35294 objects /
1668608 bytes in 93ms
04-15 16:11:05.529: DEBUG/NetworkLocationProvider(74):
onCellLocationChanged [892,8580]
04-15 16:11:55.478: DEBUG/NetworkLocationProvider(74):
onCellLocationChanged [892,8580]
04-15 16:12:45.528: DEBUG/NetworkLocationProvider(74):
onCellLocationChanged [892,8580]
04-15 16:13:35.568: DEBUG/NetworkLocationProvider(74):
onCellLocationChanged [892,8580]
04-15 16:14:05.178: INFO/ActivityManager(74): Starting activity:
Intent { act=android.intent.action.MAIN
cat=[android.intent.category.HOME] flg=0x10200000
cmp=com.android.launcher2/.Launcher }
04-15 16:14:05.209: DEBUG/Interstitial(2477): onPause

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to