Android Background Geolocation Service Without Any Kill | Part 1

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.

Google Android’s location services can also be used to track real-time, we can get real-time coordinates of device, speed, address, accuracy etc, this makes it possible to create tracking application for an example Uber and OLA uses these to track nearby cars, food delivery services to track your food.

But what if we want to track a device even if an application is closed or killed? Is it possible to track such device? Yes! its possible today’s we will create an Android Native application using location services which can track location even if the application is not open or closed intentionally, and even killed by a user.

Let’s get started!

Here we will start with a new Android project having Empty Activity

Open project’s AndroidManifest.xml file and add permissions for Internet and Location Access

Here we will also add a Broadcast Receiver “LocationUpdatesBroadcastReceiver”. This BR will communicate with device’s Location Service through a Pending Intent which we will create in coming steps.

So our AndroidManifest.xml will finally look like this

In build.gradle(Module:app) file will add Google play services required to use location services, XDroid’s toaster(optional, but I am using this third-party package to show toasts) and Android’s design package v 27.1.1 to show Snackbars.

Here we need to change the default value of minSdkVersion to 16 from 15, as 15 SDK doesn’t support notification expand for big text.

Let’s move to MainActivity.java, here we will do many things:

First, when the application initializes or onCreate method is called, we will check if the device is having proper permissions? If not we will show a permission acceptance box to accept, otherwise a Snackbar will show up continuously to enable location permission.

FusedLocationProviderClient is getting access from Google API to use Location services, We use Fused Location Client after 11.0.1 version of Google Play services. This is more stable and healthy in terms of Device resource management and battery conservation.

Using LocationRequest, for setting the preference to fetch Device coordinates:

setInterval: Gap between location updates.
setFastestInterval: Shortest gap possible for real-time updates.
setPriority: Choose from available options, in terms of battery consumption and accuracy of device location.
setSmallestDisplacement: Distance in feet only after which next update will be available.
setMaxWaitTime: Maximum time before wich update must be sent.

You can read more about these on official documentation by Google

PendingIntent will be given to a Broadcast Receiver which in turn will look for location updates. We can simply start and stop out a PendingIntent look using Broadcast Receiver. This will play a key roll in keeping our Location Service in Background alive.

Why not a Service instead of Broadcast Receiver?

We can do that using a service but in future Android API’s we will have limited slots from Service Threads available due to betterment in resource management.

We also have two buttons START TRACKING and STOP TRACKING and a text to show Location and Address in application’s Main Activity.

So our final MainActivity.java is follows

Create a new class Utils.java under root package, this class will have getLocationUpdates which will be called from Location Broadcast Receiver to get device details like Lattitude, Longitude, Speed, Direction, Accuracy etc.

On getting called getLocationUpdate method will pass on all values to a method showNotificationOngoing which will show custom notification real-time with information like time and address related to lattitude and longitude coordinates recieved. These notifications will be frozen type and can’t be removed by swiping, as we have set  .setOngoing(true) to true

We also have removeNotification method to remove notification when location service is intentionally stopped by the user.

Use of Geocoder Service

We have also used Geocoder Service by Google to convert Lattitude and Longitude in a human-readable address. This makes it more convenient from the user point of view.

The getAddress method is converting an array of location address into comma separated string which we will show in Notification and In application.

final Utils.java file will look like this.

Find source code in part 2 of this tutorial here

Go to part 2 of this tutorial

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Ravi Dharwad Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Ravi Dharwad
Guest
Ravi Dharwad

Thank you for such a good example, one thing that I observed that it will not start if my device is rebooted. What changes should I do to this for starting application on device reboot?