A easy implementation
Have you ever ever questioned how some apps like Spotify, Google Maps, and Google Meet proceed to run even after the person/system has killed the app? Nicely, you guessed it, it’s due to foreground companies.
We’ll be constructing a easy stopwatch app that can use a service to energy all of the stopwatch capabilities. As quickly because the app goes to the background we’ll convert the background service to a foreground service in order that our stopwatch can nonetheless operate though the app is within the background.
The app that we’ll be constructing in motion:
What the heck even is a foreground service?
So that is what the official Android documentation states:
Foreground companies carry out operations which can be noticeable to the person.
Foreground companies present a status bar notification, in order that customers are actively conscious that your app is performing a activity within the foreground and is consuming system sources. The notification can’t be dismissed except the service is both stopped or faraway from the foreground.
I’ll make it a bit clearer for you.
Any service can carry out background duties, and that’s fantastic so long as your app is seen to the person, however as soon as the app is killed by the person or is idle for a very long time, Android would possibly resolve to kill the method and this may result in your service being killed in flip.
To unravel this drawback we use foreground companies which be certain that our service is just not killed by Android when the app is in background.
Resulting from safety causes, a foreground service at all times must show a notification which can’t be dismissed till the duty is full in order that the person is conscious that one thing is occurring within the background
Time to construct the Stopwatch App
Sure, we’re completed with the idea, now let’s construct the app.
First, let’s add the permission required for utilizing foreground companies.
Add the next line in your
Right here’s the structure file which is able to render our Stopwatch UI:
Now we’ll begin developing the
StopwatchService which is able to deal with the core logic of the functioning of the stopwatch.
Creating the service class:
We now have to let the Android system know that we have now a service. To do this we’ll add this to our
AndroidManifest.xml below the appliance tag:
Let’s create some constants in StopwatchService.kt that can assist us with speaking with the service:
The Service Class requires us to override two capabilities. Let’s try this now.
Since Android 8.0 we have to create a notification channel.
And whereas we’re at it, we will even create a operate to get entry to the system’s
NotificationManager for all future use.
Now let’s outline every operate and arrange their performance.
We now have efficiently carried out all of the performance that might enable us to make use of this service to run the Stopwatch so long as the app is alive.
However now let’s implement the capabilities which might enable us to construct, replace the notifications and begin/cease the foreground service.
Now let’s use the service within the MainActivity to make the app purposeful
Defining two Broadcast Receivers. One is accountable for receiving the standing of the stopwatch and the opposite one is accountable for receiving the elapsed time.
A broadcast receiver (receiver) is an Android part which lets you register for system or software occasions. All registered receivers for an occasion are notified by the Android runtime as soon as this occasion occurs.
onResume we’ll initialize the
BroadcastReceivers within the following means:
updateStopwatchValue are used to replace the structure with the suitable values obtained from the Broadcast Receivers
An Android part (service, receiver, exercise) can set off the execution of a service through the
startService(intent) methodology known as and the service is just not but working, the service object is created and the
onCreate() methodology of the service known as.
As soon as the service is began, the
onStartCommand(intent) methodology within the service known as. It passes within the Intent object from the
Let’s outline and add all of the capabilities that shall be used to speak with the service:
Triggering the suitable operate on button clicks within the
Now for the final half, we have to name the
moveToBackground capabilities from
onStart respectively to start out and cease the foreground service duly.