Realtime Location Sharing with Firebase and Android Background Service

Realtime Location Sharing with Firebase and Android Background Service
What are you going to learn?

Build an application which stores the current location of the rider in firebase real-time database and display the live location of the rider to the customer on the map using Google Map SDK and Android background service.

Services in Android
  1. Background service

    A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.

  2. Foreground service

    A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting with the app.

    When you use a foreground service, you must display a notification so that users are actively aware that the service is running. This notification cannot be dismissed unless the service is either stopped or removed from the foreground.

Why to use the foreground service for fetching the current location in android?

Whenever an app runs in the background, it consumes some of the device's limited resources, like RAM. This can result in an impaired user experience, especially if the user is using a resource-intensive app, such as playing a game or watching a video. Android 8.0 (API level 26) imposes limitations on what apps can do while running in the background to improve the user experience. Hence to prevent the Service from being killed by the system we need to use foreground service.

Android Implementation

We will build this app using Android LocationManager API, Google Maps API, and Firebase Realtime Database. We will track the user's real-time location with Android’s LocationManager API and push the location update via Firebase realtime database. We will stream the location updates on Google Maps using Google Maps API.

Project setup
  1. Create a new firebase app.
  2. Create a new project in Google Maps Platform and generate a new key.
  3. Connect Android project to Firebase.
  4. Create a real-time database instance in Firebase and update security rules.
  5. Create Android App project (Rider/Customer).
  6. Add Google play service and Firebase database dependencies in app’s gradle file.
  7. Add Internet and Location permissions in AndroidManifest.xml
Rider App

A rider app will broadcast the live location of the Rider.

1. Location Service

We will create a foreground service and initialize the firebase database and display the foreground notification. Then onStart command we will start location updates and save the lat/long to the firebase database.

Note: Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel.

You must need to register your service in AndroidManifest file and add the permission for foreground service for Android Pie.

2. How to get live location updates?

Before requesting location updates you must need to check location permission.

We will use the best location provider such as GPS for live location, hence we need to make sure GPS is enabled in the user’s device. If GPS is not enabled, show a popup dialog to enable it.

This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to be notified when the device enters the proximity of a given geographical location.
Criteria class is used for selecting the best location provider. Providers may be chosen according to accuracy, power usage, ability to report altitude, speed, bearing, and monetary cost.
Request Location Update
locationManager.requestLocationUpdate (String provider, long minTime, float minDistance, LocationListener listener)
  • provider: The name of the provider with which to register
  • minTime: Minimum time interval between location updates, in milliseconds
  • minDistance: Minimum distance between location updated, in meters
  • listener: A LocationListener whose OnLocationChanged method will be called for each location update
3. How to save data to the Firebase realtime database?
4. How to start service from activity?

To start our location service we can use startService() orstartForegroundService() methods from our Activity

Similarly, we will use method stopService(Intent service) to stop the location service

Customer App

Now we will create a customer App which will display the live location of the Rider on the Map.

onMapReady(GoogleMap) method is triggered when MapActivity opens which contains an instance of GoogleMap and a method to fetch updated latitude and longitude from the firebase realtime database.

A polyline is a list of points, where line segments are drawn between consecutive points.

We will use it to draw the path of the rider.

1. Receiving location updated from firebase database?

Whenever there is a change in the realtime database it will call onDataChange(dataSnapshot : DataSnapshot) of addValueEventListener(ValueEventListener) listener which is of type ValueEventListener.

2. Update map with the latest current location

After receiving a location update, we will update the marker on the Map.

3. How to convert a vector image to a BitmapDescriptor?

We used the custom image as a marker icon in the Google Map and it is a vector image but the marker icon method needs BitmapDescriptor so the image needs to be converted to a BitmapDescriptor.

Hurray !, it’s completed now. That’s how you can use the live location tracking feature in your android application using firebase, google play service, and service in android.