This Android Location Services helper class will allow you to quickly and easily retrieve location data in your Android application.

Android Icon

 

To utilise this code create a new class in your Android project called LocationHelper and paste the code from this link in to it.

Android Location Services

 

Then, when you want to get a users location in your application create an instance of the class, for example:

LocationHelper myLocationHelper = new LocationHelper(this);

 

The above line of code declares an object of the LocationHelper class and instantiates it. The helper class is now attempting to acquire the users location data. You can check whether or not a location has been acquired using the gotLocation() method.

boolean gotLocation = myLocationHelper.gotLocation();

 

This will return true if the helper has found a location or false if it hasn't got one yet. Don't forget acquiring a users location can take some time depending on whether or not you are waiting for a GPS lock. This means it is best to call gotLocation() in a loop whilst waiting for it to return true. As locking the UI thread is a bad idea whilst waiting for a result that could take some time this is best done in a worker thread using an AsyncTask. Declare this class as a sub class inside the activity where you want to get the users location data:

Android Location Worker Thread

 

Once started, the AsyncTask will wait for the Location Helper to acquire a location but it will do so on a worker thread instead of locking the UI thread which is bad practise. You can create an instance of the worker thread using the following code:

//create new async task for fetching location and execute it
LocationWorker locationTask = new LocationWorker();
locationTask .execute(new Boolean[] {true});

 

In the onPostExecute() method of the AsyncTask you can access the location data and do what ever it is you need to do with it. You can store it in local variables or populate a view in your application with it. You can also call these methods from anywhere the location helper is in scope.

myView.setText(String.valueOf(myLocationHelper.getLat()));

or

longitude = myLocationHelper.getLong();

 

At present the Location Helper is set to get the users location and then stop monitoring location data as this consumes power. If you want the service to continue monitoring, so that each time you call getLat() or getLong() you get updated data, you can comment out the following line in the onLocationChanged() method:

locationManager.removeUpdates(locationListener);

 

This will allow the helper class to continue receiving updated location data but when you are finished with it you must call killLocationServices() on the helper to stop receiving updates so you don't unnecessarily drain the users battery.

myLocationHelper.killLocationServices();

 

I hope this code is helpful and if you do use it please link back to this page! Comments and questions in the section below.

 

Scott.

 

*** UPDATE ***

You can check to see if the device has any location services enabled using the following code. This will fecth the current location providers that are enabled:

// get location providers
String locationProviders = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

 

You can then check specific location data providers by checking for a GPS provider or a Network provider (which includes GPRS and WiFi). The following code will check to see which are enabled and output messages to the user if they need to enable any location providers. You can edit the code to take different action depending on which providers are enabled and disabled:

Location Services Check

Hope this helps!

 

Scott.
Short URL: https://scotthel.me/AndroidGPS