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

Going mobile Part 3 – This blog is now mobile enabled!

The Page Not Found Blog is now mobile enabled! Please send me your feedback. After months of research and several months of fiddling around the mobile template is now live. You should only see the template if you are using a mobile device.

As I’ve mentioned before, I’ve taken the approach of using two different templates for a single website. One is specifically for desktop browsers and one is specifically for mobile. It’s all an experiment anyway, so whatever happens is a learning experience.

The current mobile template settings are fairly bare bones, but I did that on purpose to speed up load times. I’ll tweak the look and feel as time goes on.

There are also still a few rough edges related to my legacy content that I personally noticed. For example, some images are displayed in odd places when viewed on mobile browsers. But, so far the WordPress tags seems to be displaying just fine, which was a major concern for things such as code snippets. I’ve tested the site using an Android v4.x device and an iPad 3.

References

Going Mobile with Blogging

Going Mobile Part 2 – Dealing with legacy content

Smartphones greatness hampered by limited cell and internet service

Compared to good old land-line phones, smartphone reliability is mediocre at best because it largely depends on you always being connected to the internet. This post brings to light that we’ve traded massively reliable twisted pair telephone service and mostly reliable home internet for something that, at least to me, is barely tolerable and only just “good enough”. Smartphones have become popular in spite of connectivity issues. Where are still in the stone age of cellular service.

How dependable? You could “always” depend on the old fashioned, hard-wired land-line phones to work even during natural disasters. In fact, I once grabbed a pair of phone wires from the rubble a house completely destroyed by a Class 5 tornado and was able to use a field phone to make a call. Try to do that with a smartphone! Nowadays, most of what we do with our smartphones is 100% dependant on an internet and cellular service connection. And, it’s not like you are dragging around a dedicated T1 service and ethernet cable where ever you go. If the cellular connection is bad we either have to wait until we can find a Starbucks with WiFi or simply go without phone calls, text messages, emails and surfing the internet. In fact even Starbucks connectivity can be spotty if there are a lot of people at Starbucks working on uploading or viewing their family Christmas picture albums. 

No internet connection? Sorry, but you won’t able to use Skype and gasp…you’ll have to use your cell phone minutes to make a voice call.

Overloaded system. Cellular companies are making it even more challenging with tiered pricing for internet usage, phone plans that don’t include roaming, and congested networks that are sloooowly being upgraded. They say these pricing approaches help maximize existing bandwidth for everyone, or perhaps it’s to help them maximize revenue?

It also seems that the rate of smartphone adoption may have outpaced the networks ability to keep up in many dense urban areas and outside of those areas away from major highways you may just be stuck without cellular service if you don’t have a landline.

Spotty coverage areas. There are certain parts of my house and certain areas at work that have dead zones to cell service. I work in a densely populated urban area and some co-workers, depending on which cell service they use, have to step outside to get a “good enough” signal to carry on a conversation. It’s becoming more common for cell providers to offer private amplification repeaters that improve service to a specific house or building, and the funny thing is these rely on good old land-line connections.

How about in emergencies and disasters? If a cell tower goes down then all communication is wiped out completely…remember Hurricane Katrina and Sandy? If/when cell service is restored and you still don’t have power at your house, you will have to turn off your phone and use its remaining battery life wisely. Turn it on, make a quick call or send/receive text messages and then turn it off again. And, when the battery is dead…it will stay dead until you find a working power outlet.

What’s the future hold? What I’d really like to see is guaranteed service, like the uptime agreements we used to have with T1s, T2s and T3s. You could expect a minimum amount of service and generally speaking the provider would try and meet those conditions. With the way cell service is distributed today it’s anyone’s guess whether you have access to cell or internet at any particular point in time.

Probably the best thing to do is call your provider and complain when coverage needs improvement. Most companies seems to document these issues although its not clear who looks at that information. It seems reasonable however that the more people giving feedback the better information the cell providers will have to make decisions.