• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Question regarding the Course and Fine location providers.

MrRalphMan

Newbie
Hi,

I'm trying to write an application that uses the inbuilt location providers to provide location, speed etc.

I am attempting to register two providers, the Fine one to use the GPS and the Course one to get a lock and details prior to the GPS one getting an lock.

I have a couple of questions.

With the two location providers registered to give regular updates, should they both fire or is it only the best one, GPS (Fine) in this case, that fires?

The location.HasAccuracy is set to true for both location managers, but the Course one never seems to fire.

Many Thanks,

Paul.
 
Depending on how you implemented it, you will only get the location events in one place. Show us your code (in forum code tags, please), and it'll be easier to help...

Anyways, below is how I usually do it, using both Coarse (Network) and Fine (GPS) location. Both are registered with the LocationManager and handled by the same LocationListener.

Code:
private void initLocationManagement(){
		locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locLis = new LocationListener() {
			public void onStatusChanged(String provider, int status, Bundle extras) {	
			}
			public void onProviderEnabled(String provider) {	
			}
			public void onProviderDisabled(String provider) {	
			}
			public void onLocationChanged(Location location) {
				updateLocation(location);
			}
		};
		locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locLis);
		locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLis);
	}

If you need to do separate things, depending on the source of the incoming location, you can just register to different LocationListeners - but keep in mind that you'll always get your location from the GPS provider if the phones GPS is turned on - it might not be precise, but using A-GPS enables the GPS to get the position from the mobile network when no real GPS fix is available, resulting in the network location coming from the GPS.
 
I used some code from an example I found, the raw code is as follows.

Code:
private void registerLocationListeners() {
        locationManager = (LocationManager)
            getSystemService(LOCATION_SERVICE);

        // Initialize criteria for location providers
        Criteria fine = new Criteria();
        fine.setAccuracy(Criteria.ACCURACY_FINE);
        Criteria coarse = new Criteria();
        coarse.setAccuracy(Criteria.ACCURACY_COARSE);

        // Get at least something from the device,
        // could be very inaccurate though
        //currentLocation = locationManager.getLastKnownLocation(
            //locationManager.getBestProvider(fine, true));

        if (listenerFine == null || listenerCoarse == null)
            createLocationListeners();

        // Will keep updating about every 500 ms until
        // accuracy is about 1000 meters to get quick fix.
        locationManager.requestLocationUpdates(
            locationManager.getBestProvider(coarse, true),
            1000L, 1, listenerCoarse);
        // Will keep updating about every 500 ms until
        // accuracy is about 50 meters to get accurate fix.
        locationManager.requestLocationUpdates(
            locationManager.getBestProvider(fine, true),
            1000L, 1, listenerFine);
    }

    /**
    *     Creates LocationListeners
    */
    private void createLocationListeners() {
        listenerCoarse = new LocationListener() {
            public void onStatusChanged(String provider,
                int status, Bundle extras) {
                switch(status) {
                case LocationProvider.OUT_OF_SERVICE:
                    break;
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    break;
                case LocationProvider.AVAILABLE:
                    break;
                }
            }
            public void onProviderEnabled(String provider) {
            }
            public void onProviderDisabled(String provider) {
            }
            public void onLocationChanged(Location location) {
                <Do some stuff>
            }
        };
        listenerFine = new LocationListener() {
            public void onStatusChanged(String provider,
                int status, Bundle extras) {
                switch(status) {
                case LocationProvider.OUT_OF_SERVICE:
                    break;
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    break;
                case LocationProvider.AVAILABLE:
                    break;
                }
            }
            public void onProviderEnabled(String provider) {
            }
            public void onProviderDisabled(String provider) {
            }
            public void onLocationChanged(Location location) {
               <Do some stuff>
            }
        };
    }

Now this works, but I only get location data from the Fine GPS Service, Once when the app first starts and then when it get's a lock. The course one never seems to fire.

In your example does the Course one fire until the Fine one gets a lock?

Cheers,

Paul.
 
Try running your app with the phones GPS completely turned off!
And as I said, I don't think the LocationManager will send Coarse locations if Fine are available (GPS turned on) - that would be a terrible energy waste...

And btw, it's Coarse (with an A, as in your code) and not Course (with a U) - Just bugging me...
 
Hi,

Well made some progress.

Using pretty much the code you posted above, when running without GPS enabled, the Network provider either only fires once after about 10 seconds of the activity starting and then never again.

When running with GPS enabled, there are not updates until the GPS gets a lock and then it's once a second as expected.

The weird thing is that every so often when running on GPS I'll get a zero speed entry. This was tested today while walking, but I saw this yesterday too in the car.
The Network location setting is enabled in settings.

I'm going to do some more testing and will let you know how it goes.

Just need to find a good logcat capturer. I am using aLogCat, but it only seems to go back a short way, will see if I can get one that will log the filtered output to a file.

Many Thanks,

Paul.
 
Well... Remember that the listener is for LocationChange, so the registered location has to change before you get a new location. And for the registered network location to change, you have to move a fair distance, since it is based on the mobile network, and only precise within a km or so - hence, it wont fire until you move to another network cell. So try testing your app sometime when you move a greater distance...

For the logging part, I usually make a TextField in the app that shows the log while testing, then you can also save the text from that field to a file on the phone whenever you need...
 
Again,

Thanks for the reply. I have done some testing this morning and the Network Provider does fire, it just doesn't present me with the speed.

Attached is the logcat output from the test this morning.

This was me walking to work and the mUpdateTimeTask is fired every second to update my elapsed time field.

So it does fire every so often, but did not present me with the speed. I was under the impression that it should do.

Cheers,

Paul.
 

Attachments

Back
Top Bottom