<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

The old Android LocationListener

Published on
9,355 Points
2,255 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

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Join & Write a Comment

Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month