Various GPS Android Functionality Questions
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 开发者_JS百科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
}
}
- Lines 4-5 seem to be doing you no good. Also,
LocationManager.GPS_PROVIDER
is"gps"
, so please just useLocationManager.GPS_PROVIDER
.
For the following answers, I am assuming that by "GPS lock" you mean "GPS fix".
"Is there a way to hold off on the last step (accessing "getLastKnownLocation" until after I am sure I have a GPS lock?" -- sure, by not calling the method. "What happens if this is called and GPS is still looking for signal?" -- it returns
null
."MOST importantly, I want to ensure I have a GPS lock before I proceed to my next method" -- that is not possible. "so is there a way to check to see if GPS is locked on and getLastKnownLocation is up to date?" -- if
getLastKnownLocation()
returnsnull
, you do not have a fix.Unregister your
LocationListener
viaremoveUpdates()
.Yes, your assumption is correct, at least for some definition of "frequently".
Yes, you need a
LocationListener
instance to userequestLocationUpdates()
. More importantly, when you get called withonLocationChanged()
for the first time, that means you got your first fix. Presumably, for how you appear to want to use the code, this is where you would process the fix, then remove theLocationListener
per #4 above.
精彩评论