Various GPS Android Functionality Questions..
Posted
by Tyler
on Stack Overflow
See other posts from Stack Overflow
or by Tyler
Published on 2010-05-10T23:48:06Z
Indexed on
2010/05/10
23:54 UTC
Read the original article
Hit count: 331
Hello -
I have a few questions (so far) with the the LocationManager on Android and GPS in general.. Feel free to answer any number of the questions below, and I appreciate your help in advance! (I noticed this stuff doesn't appear to be documented very well, so hopefully these questions will help others out too!)
1) I am using the following code, but I think there may be extra fluff in here that I do not need. Can you tell me if I can delete any of this?
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
LocationProvider locationProvider = lm.getProvider("gps");
Location currentLocation = lm.getLastKnownLocation(locationProvider.getName());
2) Is there a way to hold off on the last step (accessing "getLastKnownLocation" until after I am sure I have a GPS lock? What happens if this is called and GPS is still looking for signal?
3) MOST importantly, I want to ensure I have a GPS lock before I proceed to my next method, so is there a way to check to see if GPS is locked on and getLastKnownLocation is up to date?
4) Is there a way to 'shut down' the GPS listener once it does receive a lock and getLastKnownLocation is updated? I don't see a need to keep this running for my application once I have obtained a lock..
5) Can you please confirm my assumption that "getLastKnownLocation" is updated frequently as the receiver moves?
6) In my code, I also have a class called "MyLocationListener" (code below) that I honestly just took from another example.. Is this actually needed? I assume this updates my location manager whenever the location changes, but it sure doesn't appear that there is much to the class itself!
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
//Toast.makeText(getBaseContext(), "Location changed : Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
© Stack Overflow or respective owner