0

How do I get my app to start updating GPS location as soon as I open it?

Right now I have this code:

locationManager.requestLocationUpdates(bestProvider,0 ,0, loc_listener);
location = locationManager.getLastKnownLocation (bestProvider);

Which takes the best location. I ran my app on my phone and it got the location from where I was earlier today.

I want my app to update the gps location when I first open it.

I have been looking at these threads:

What is the simplest and most robust way to get the user's current location on Android? Android find GPS location once, show loading dialog

But I am having trouble wiring everything up.

Community
  • 1
  • 1
Stagleton
  • 1,060
  • 3
  • 11
  • 35
  • "Wiring everything up" as in wire the code into an interface or as in not using first `getLastKnownLocation`, but rather last known after sat lock? – John Giotta Dec 15 '11 at 17:18

1 Answers1

1

If I'm not mistaken you need to set up a listener and override the onLocationChanged method. That should get you your next GPS location ie the location after sat lock.

This tutorial should illustrate what i'm trying to say: http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2 .

Stop me if I'm wrong StackOverflow, I'm not sure of myself on this one.

Robin Eisenberg
  • 1,836
  • 18
  • 26
  • well I got something to work and now I just need to get the GPS sensor to stop – Stagleton Dec 16 '11 at 20:16
  • unregister the listener when you are done, that should do it. A usefull little something I learned recently here on stackoverflow: new Handler().postDelayed(new Runnable() { @Override public void run() { locationManager.unregisterListener(listener); } }, theTimeInMilliseconds); This will unregister the listener, but only do it after theTimeinMilliseconds milliseconds. Should complete your answer. – Robin Eisenberg Dec 16 '11 at 22:37