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 LocationUpdatesBroadcastReceiver.java, 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.

LocationUpdatesBroadcastReceiver.java will look as below:

Next, we will create a SharedPreference Utils class named LocationRequestHelper.java.

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

LocationRequestHelper.java 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

 

 

10 thoughts on “Android Background Geolocation Service Without Any Kill | Part 2”

  1. 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.

    1. Hello Sankeerthana,
      I have added a source code Github link of sample application explained in this tutorial.
      Thanks
      Jolly

      1. There isn’t a license file with the source code. What is the licensing of the source code? Can portions of the source code be used freely in other projects? Any restrictions on it’s usage?

        1. Hi Ron, you can use this anyway you like it. If you really appreciate this project then i can add a donation link 😁😀. jusk kidding. But it took two months to make it work phewww 😊😉

  2. 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 ?

  3. 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

    1. Hi Paijo, sometimes customized Android os has it’s own optimization features, so you need to enable the “Background Autostart” enable for this application in mobile settings.

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

Leave a Reply

Your email address will not be published. Required fields are marked *