Background Location for android (Lollipop and above)

Chances are high you landed on the above page first before this article. COVID has brought out the developer in us and almost everyone wants to create an app that monitors user’s whereabouts in the background. If you are new to android development, like every optimistic programmer you start coding.

1 hour after your code sprint :)

There’s an easy workaround to the restriction shall you wish to implement it. Enter: Foreground Services. Let’s dive into the implementation.

Disclaimer: Traditionally getting the current location is implemented by Fused Location Client, but for this article let’s focus on the service. The API implements it for us in the backend along with other fallbacks.

Step 1: Install the Library

implementation 'io.nlopez.smartlocation:library:3.3.3'

Step 2: Adding permissions

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Step 3: Implementing the service class

class LocationService : Service() {
private var builder : LocationParams.Builder?=null
private var
mNotificationManager: NotificationManager? = null

override fun
onBind(intent: Intent?): IBinder {
throw UnsupportedOperationException("Not yet implemented")

override fun onCreate() {
builder = LocationParams.Builder()

mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val mChannel = NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, Constants.NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT)

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
startForeground(1, notification)
.start { location ->
Toast.makeText(this, location.latitude.toString() + " " + location.longitude.toString(), Toast.LENGTH_SHORT).show()

private val notification: Notification
get() {
val builder = NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID)



companion object{
private val TAG =
Please define the following constants when you use the code

In a gist, my onCreate does all the heavy lifting and creates a persistent notification while onStart handles getting the location and showing the toast.

Step 4: Registering the service in MainActivity

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
binding = ActivityMainBinding.inflate(layoutInflater)

if (!Helper.isLocationPermissionGranted(this))


private fun registerService(){
val serviceIntent = Intent(this,
private fun requestPermissions(){/**handle request permission and onRequest permission result accoringly*/}

Step 5: Registering the service in Manifest

android:foregroundServiceType="location" />

There you have it. Persistent background location in the background. I’ll love to keep the article updated in case there is something that I missed.

Stay Safe❤




Sometimes it is the people no one can imagine anything of, do the things no one can imagine.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building a successful cloud business in an on-premise dominant company

Vim: 2x 5x 10x Your Productivity

Open my home Kubernetes cluster to internet and secure it with Let’s Encrypt TLS certificate

Roll your own Godmode Ganache CLI server on AWS

Photo by Kvistholt Photography on Unsplash

From Fibonacci to N-bonacci

The Delicate Art of Making Robots That Don’t Creep People Out

How to use Open Application Model to run applications on Kubernetes

The Simplest Ways To Make The Best Of Siteground Hosting

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Sometimes it is the people no one can imagine anything of, do the things no one can imagine.

More from Medium

5 Ways of Creating Android LiveData

Handling Component Changes Affected by Theme on Android

Getting started on Android App Development

Android Application Components