Android Background Geolocation Service Without Any Kill | Part 2

Android location services are very popular these days, they provide the location of a device in the form of latitude and longitude coordinates using with we can track the location up to 10 meters of accuracy. Location services provide many other parameters of a device like speed, the direction of movement, human-readable address using Geocoder service.



This tutorial is the second part of the previous one, please visit part 1 here before going through this post.


Let’s continue on the implementation of Background Geolocation Service in Android Device.

Create a Location BroadcastReciever 

Create a new broadcast and name it, in this file, we will onReceive method which will be called and in turn, will call the getLocationUpdates method in Utils class.

The setLocationUpdatesResult method will change in SharedPreference value to update text having the address and other details when the application is open. will look as below:

Next, we will create a SharedPreference Utils class named

What is SharedPreference in Android?

If you have a relatively small collection of key-values that you’d like to save, you should use the SharedPreferences APIs. A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.

This file is having some methods to save and get the different type of data like string, boolean, object etc, we have not used all of them but it’s better to keep all of them file will look this

Update activity_main.xml layout file under res>layout folder in the project with the following code



Finally, our project directory structure will look like this

Now run the application in a real device to check if it’s working as expected. Let me know if you face some issue regarding the same.

Find full sample source code on Github here



Leave a Reply

6 Comment threads
6 Thread replies
Most reacted comment
Hottest comment thread
9 Comment authors
SebastianPabloEddy DavilaPaijo Tukinoakshay shahane Recent comment authors
newest oldest most voted
Notify of
Bhushan Shirwalkar

awesome tutorial..was looking for it since long time.. Can you please (if possible after starting updates) also add checking if location service is off after starting location updates. It asks to start location service before starting location updates (as usually) but when we switch off device location after starting location updates It doesn’t check if device location is off and continue to receive location (may be) with network or nearby wifi (as FusedLocation api does usually). Thanks.


Can I get full source code?

akshay shahane
akshay shahane

I only receive 10-15 location updates with this code. My question is there are limitations to location updates every hour how getting location updates through pending intent solves that ?

Paijo Tukino

I tried this and the apps stop broadcasting my current location after 40-50 minutes on first attempt. On second attempt (I press stop tracking and start again), it only works for about 20 minutes. Any suggestion what’s wrong ? I just cloned and compiled the source from gitHub

Eddy Davila

Fantastic! I have a small issue..when i close the app updates stop being sent. Is there a reason for this? Thanks


Good example using the fused location, but the key points (at least for me) – getting the location in real-time without being killed by the OS were berried in too much additional code, with which figuring out where the magic is is quite challenging. More than 75% of this code can be scrapped away and you will still get a perfectly running solution serving its main purpose – report you location in real-time without receiving kills.


Could you maybe let us know where the magic is happening so?