Affiliate links on Android Authority may earn us a commission.Learn more.

Using the ThingSpeak API to track a device’s location

July 07, 2025

We have previously demonstratedhow to use a web API from your Android app, and we have also discussedhow to get and use location data in your Android app. In this tutorial, we will utilize both of these lessons, and show how you can track a device’s location using a remote web API. The remote web API we selected is theThingspeak API.

For this article, we will develop an Android application that gets the device’s location information, and sends the latitude/longitude data to a ThingSpeak channel. We will then fetch the last location data from the ThingSpeak channel, and compute the distance from the last updated location to a location entered by the user. A simple (and obvious) use of this is parents who wish to track their children, or employers tracking employees. The ThingSpeak channel can also be viewed using a browser.

Article image

Getting Started

First thing you should do is toregister for a ThingSpeak account. It is, at the time of writing, completely free. After registration, you will need to create a channel. you’re able to have multiple channels, but each channel has a unique ID, and a special Channel write API_KEY, that can be used to write or read data to the channel. For a private channel, you can generate read API_KEYs, that can only read data from the channel.

While creating your channel, you might notice a Latitude and Longitude field. These are used for static devices, and are not suitable for our use case. ThingSpeak allows for the tracking of up to eight different fields, named field1 through field8. We add latitude as field1, and longitude as field2. You can access the ThingSpeak documentation online on theirSupport page

aa_thingspeak_fields

App Layout

Our app layout displays the current device latitude and longitude. Beneath these is a button, enabling us begin and pause location tracking. Whenever the app receives a location update, the values of the TextViews are updated, and the new location data is immediately sent to the ThingSpeak channel. There are two EditTexts, into which the user can enter latitude and longitude values to compare against the device’s location. Clicking on the “Get Distance” button will fetch the last updated location from the thingspeak channel, and perform aLocation.distanceTo()to compute the distance between both Locations.Our activity_main.xml is a simple RelativeLayout.

Updating your channel

Recall our previous article onfetching location data? For this app, we are using the GPS_PROVIDER. If your application runs in the background (as a service), consider using the PASSIVE_PROVIDER, or use a longer refresh time.

In the code snippet below, when the “Begin Tracking” button is clicked, we request location updates from the GPS_PROVIDER every two minutes (2 * 60 * 1000). However, if the button reads “Pause Tracking”, we ask the LocationManager to stop sending updates.

aa_thingspeak_layout

This snippet is our implementation of the LocationListener interface. When we receive a new location update, the onLocationChanged() method is called, with the new Location information as the method arguments. On extracting the latitude and longitude values, we execute the AsyncTask that updates the thingspeak API.

Android prevents developers from performing potentially long running tasks in the UI thread (the thread that controls the responsiveness of your application’s widgets). As such, we must perform network tasks in another thread, and the AsyncTask is a great class that handles all the thread complexity behind the scenes. Instructions in both onPreExecute() and onPostExecute() are run on the UI thread before and after the long running task respectively, while the long running task is placed in the doInBackground() method.

aa_thingspeak_newyork

Fetching channel data

There are many different ways data can be read from a ThingSpeak channel. This includes

Consult thedocumentationto learn more.

We, however, are interested in retrieving the last update only. This is implemented using an AsyncTask.

In the onPreExecute() method, we fetch the latitude/longitude values, disable further editing of the fields, and display the ProgressBar.

aa_thingspeak_high_battery

In the onPostExecute() method, we parse the received to a JSONObject, fetch the returned latitude/longitude values, and finally compute the distance between both locations.

The complete source isavailable on Github, for use as you see fit. If building from scratch, you would need to request for both the INTERNET and LOCATION permissions by adding the following to your AndroidManifest

Using the GPS_PROVIDER is a battery hog, so if your app is going to run in the background, consider using the PASSIVE_PROVIDER to reduce battery usage.

The ThingSpeak API allows channel updates every 15 seconds, and fields can contain either numeric data or alphanumeric strings. There are tons of interesting public channels, including ahamsteronTwitter. Have you used ThingSpeak or a similar service? What was your experience? What was/is your use case? Share in the comments below.

Thank you for being part of our community. Read ourComment Policybefore posting.