private class My_listener
{
LocationListener ll = new LocationListener()
{
};
}
private class My_listener
{
LocationListener ll = new LocationListener()
{
@Override public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
}
@Override public void onStatusChanged(String provider, int status,
Bundle extras)
{
// TODO Auto-generated method stub
}
@Override public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
}
@Override public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
}
};
}
private class My_listener
{
LocationListener ll = new LocationListener()
{
@Override public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
}
@Override public void onStatusChanged(String provider, int status,
Bundle extras)
{
// TODO Auto-generated method stub
}
@Override public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
}
@Override public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
}
};
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
Once again the IDE will prompt you to import the libraries. You now appear to have class that just needs the addition of a couple of doubles to store the devices whereabouts.
@Override public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
double lat = location.GetLatitude();
double lon = location.GetLongitude();
}
What I found was that this method was never called, even during start up. I could not find a solution in the documentation and other users had logged this as a bug, more than once, on the Google fora. Rather unhelpfully the administrators just closed the bug without a resolution saying there was not a problem. What they, and the documentation, do not mention is that you cannot have a silent
LocationListener; it must be implemented by the class, like so:
private class My_listener implements LocationListener //The activity implements the listener
{
@Override public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
double lat = location.getLatitude();
double lon = location.getLongitude();
}
@Override public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}
@Override public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
}
@Override public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
}
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
It does not look like much, but I had to spend a couple of days figuring this out. Hopefully this will prevent the same frustration in others.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)