3 Steps for Shutting off GPS and LocationManager on Android

While the code for shutting off the LocationManager on Android is straight forward, sometimes just one misstep and you can spend hours trying to figure out why it doesn’t work. What will happen is you will see in your code that the LocationManager and perhaps even the listeners will be null and the GPS active icon will continue to display and your battery will drain faster than you ever thought possible. There are a bunch of comments already on Stack Overflow about this problem, so this post attempts to consolidate answers and provide some straight forward steps to help with the most common mistakes.

First let’s review the steps for properly shutting LocationManager down:

Step 1. Remove the listener or listeners. If you are using both GPS and network providers you have to remove both. It sounds oversimplified, but you can only remove the exact listener that is associated with a particular instance of the LocationManager. It’s a one-to-one relationship.

Step 2. Set LocationManager to null.

Step 3. Set listeners to null.

Here’s what the code looks like, the names should be self explanatory:

if(_locationManager != null){

    _locationManager.removeUpdates(_locationListenerGPSProvider);

    _locationManager.removeUpdates(_locationListenerNetworkProvider);

    _locationManager = null;

}

if(_locationListenerGPSProvider != null) {

    _locationListenerGPSProvider = null;

}

if(_locationListenerNetworkProvider != null){

    _locationListenerNetworkProvider = null;

}

If these steps don’t work and the GPS icon continues to display, then there are potentially two reasons why:

Reason 1. You have instantiated intentionally or accidentally more than one copy of LocationManager and/or a listener. This is by far the most common and frustrating reason. The easiest way to avoid this mistake is to only start the LocationManager via the onResume() event, and shut it down using onPause(). Carefully control what is allowed to start and stop it. Be mindful when passing around instances of LocationManager between different Activities whether it is a singleton, static class or simply a reference to the actual object. If LocationManager is already running and you spin up a new instance, then you may not be able to shut it off programmatically and you’ll have to manually shut it off.

Reason 2. Something else other than your app is causing the LocationManager to run. This can easily give the appearance that it has not been turned off via your app. Repeated testing can eliminate this as a possibility, and to make sure you can shut off all other non-essential applications that use location.

References

Android Activity and Life Cycle

Tags:

One Response to “3 Steps for Shutting off GPS and LocationManager on Android”

  1. Shane Phelan says:

    Hi, great article.

    One other reason you might want to add (which helped me!) is if you’re using MyLocationOverlay and have called enableMyLocation then you should also call disableMyLocation in the onPause function.

    The MyLocationOverlay continues to poll for location changes so until disableMyLocation is called, the gps won’t shut off.

    Cheers

    Shane