The Last Known Location - Don't get the broadcast


#1

I turned off the TestProvider and clicked the Start button. I expected both of the LocationReceiver.onReceive() methods to get the broadcast sent by RunManager, but neither does. I thought this was how the GPS service would broadcast its location updates.

On the emulator, lastKnown = null. So, I modified RunManager with code from TestProvider:

[code] public void startLocationUpdates() {
String provider = LocationManager.GPS_PROVIDER;

	// If you have the test provider and it's enabled, use it
	if (mLocationManager.getProvider(TEST_PROVIDER) != null && mLocationManager.isProviderEnabled(TEST_PROVIDER)) {
		provider = TEST_PROVIDER;
	}
	Log.d(TAG, "Using provider " + provider);

	// Get the last known location and broadcast it if you have one
	Location lastKnown = mLocationManager.getLastKnownLocation(provider);
	if (lastKnown != null) {
		// Reset the time to now
		lastKnown.setTime(System.currentTimeMillis());
	} else {
		lastKnown = new Location(provider);
		lastKnown.setLatitude(66.751459);
		lastKnown.setLongitude(100.3238770);
		lastKnown.setAltitude(444);
		lastKnown.setTime(System.currentTimeMillis());
	}
	broadcastLocation(lastKnown);

	// Start updates from the location manager
	PendingIntent pi = getLocationPendingIntent(true);
	mLocationManager.requestLocationUpdates(provider, 5000, 0, pi);
}[/code]

But this doesn’t work if I use TestProvider or not.

[color=#FF0000][size=150]
OOPS![/size][/color] Gotta put the code to send the broadcast BELOW the code to “requestLocationUpdates”. Now it works.


#2

Glad you found the fix!

Make sure you’re able to get a fix when you use the GPS provider, by the way. You can subscribe to it and it’ll just sit there when you’re inside, not being able to do anything.