Android Location Management and Threading


Welcome to Part 3 of this review of the Pluralsight course Android Location-Based Apps by Jim Wilson

Jim has over 30 years of software engineering experience, with the past 15 years heavily focused on creating mobile device and location-based solutions. He has co-founded multiple software-related startups and has served in a consulting role at several more.

After nearly a decade as a Microsoft Device Application Development MVP, Jim now focuses on developing Android and iOS device applications. Jim’s passion is mentoring software developers. He blogs at

Also in this series:

Part 1 – Android Location-Based Apps

Part 2 – Android Location Providers

Part 3 – Android Location Management and Threading

Part 4 – Controlling Android Location Frequency

Part 5 – Human Readable Location Information

Android Location Management and Threading

Moving location off main thread

Jim explains that there is a lot of processing that can go on with the Location Providers, and the default behavior is for all of this to happen on your application’s main thread.

This can lead to substantial performance problems.

The main thread is responsible for all User Interface related work. It also deals with many application housekeeping tasks, Intent handling, dispatching to services and broadcast receivers etc.

All of these things add up, and slow down your application performance.

So we want to move the location processing work off of the main application thread. This is done using the Looper class, which represents standard Android message loop processing.


Demo: Moving location off main thread

We start off with some existing code in, and this file will be used throughout this module of the course.

The code initially does all the tracking on the main thread.

Jim begins by updating doStartTracking(), adding a handler thread, starting it, and getting the looper.

We can then pass the looper into our location provider methods as a parameter.

Jim also explains how, why and when to shut down the message pump.

Updating UI from location thread

So far it’s all been pretty easy, but Jim warns that our changes create challenges for the UI.

We can’t simply update the user interface from any old thread: it can only be updated from the UI thread.

To update the UI is a cross thread-safe way, we can use Activity.runOnUiThread.

Jim also discusses techniques for dealing with these problems:

  • Difficulties keeping UI in sync with location listener
  • Post API 13 Fragments can help
  • Pre API 13 must explicity manage location and thread objects

Background location tracking

In many cases we want the location tracking to go on even when the user has closed the app and switched to another app.

To achieve this we can use Android services to run things in the background.

We still need to use a Looper to avoid burdening the main app thread.

Jim covers Android Services in much more depth in his course Android Async Programming and Services.

Demo: Background location tracking

Jim starts with a stubbed out service, with an onStartCommand.

Even though the service is very distinct from the activities in the application, it’s still running on the same main thread.

Jim shows how to setup the looper so that it’s lifetime is tied to the lifetime of the service.

We learn that we should always use START_STICKY.

For further details read the docs on Android Service Lifecycle

Continue to Part 4 – Controlling Android Location Frequency

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s