<

The old Android LocationListener

Published on
8,982 Points
1,882 Views
1 Endorsement
Last Modified:
Approved
Chris Harte
A developer with over twenty years experience. Started on mainframes and Cobol, now on mobiles and Android.
A short article about a problem I had getting the GPS LocationListener working.
There are lots of reasons for wanting to know where a device is at any given time. This can be achieved by using the Global Positioning System (GPS) built into most devices. This method does not use valuable data bandwidth and does not require a Wi-Fi connection.

I recently wrote an Android application that gave the weather in the user's locality. Before I could query the weather API I had to know where the device was, so I used the old location listener. Google has a new, more accurate geo-locator that requires a subscription to a service. Personally I am not that comfortable with Google tracking the location of every user of an app written by me (even though the probability is that nobody will use it).

The old location listener is still supported and looks like it will continue to be so for a while; at least it has not been deprecated yet. It was whilst trying to use the old listener that I ran into some difficulty implementing it. I could not get the method onLocationChanged to be called. After much debugging and Googling (oh the irony) I found that there were at least two bugs logged against this very problem. Here is how I solved it.

When developing an Android app it is sensible to use a proper Integrated Development Environment (IDE). I use Eclipse with the Android Development Tools (ADT) because I use Eclipse for all my programming needs. An IDE will do a lot of spade work for you and make sure you get all the correct methods for each class in place.

First, the problem. Create a new class that implements the location listener. LocationListener is not a class but an interface. This means it cannot be instantiated, only inherited.
 
private class My_listener
{
    LocationListener ll = new LocationListener()
    {
            
    };
        
}

Open in new window


At this point the IDE will ask you to import the necessary libraries for LocationListener. Once you have done that it will then prompt you to add the necessary unimplemented methods, that, once implemented, will look like this
 
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
                
            }
            
    };
        
}

Open in new window



Then add your variables for location and your location manager.
 
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);

        
}

Open in new window

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();
                
            }

Open in new window

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);
}

Open in new window

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.
 
1
Comment
Author:Chris Harte
0 Comments

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month