Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we’ll learn about foreground Services and how to use them!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
When you play music on your phone, it's
typically accompanied by a notification.
0:00
This notification usually tells
you what song is playing and
0:05
provides some basic functionality,
like play and pause.
0:09
But this notification also
serves another purpose.
0:12
If you want your service
to run in the foreground,
0:16
then you need to have a notification.
0:19
A foreground service is a service
that the user is actively aware of.
0:21
Thus a foreground service
is given special priority
0:25
when Android encounters
low memory situations.
0:29
Remember the various levels
of importance for processes?
0:32
Well, when we designate our
service as a foreground service,
0:36
it's automatically part of the top group,
foreground processes.
0:39
And it's extremely unlikely that our
services process will be killed.
0:44
To make our service a foreground service,
all we need to do is call start
0:48
foreground from our service and
provide a notification along with an id.
0:53
Let's see how to do that.
0:57
Let's start mPlayerService by creating
0:59
a notification.builder object
at the top of OnStart command.
1:03
Notification.Builder and
let's call it notificationBuilder and
1:08
set it equal to new Notification.Builder,
1:16
and pass in this for the context.
1:21
Next let's call
setSmallIcon on our builder
1:26
and pass in R.mipmap.ic_launcher
1:33
to set our notifications icon to be
the default icon included in our app.
1:38
Then let's create a new notification,
Notification, and
1:44
we'll call it notification and
1:48
set it equal to notificationBuilder.build.
1:52
Next let's call startForeground and
pass an 11 for
1:57
the first parameter and
our notification for the second.
2:01
The first parameter is
an id that we choose,
2:06
we just need to not reuse
notification ids in the same app.
2:09
Since this is our only notification, we'll
be fine just picking any random number,
2:14
like 11, just don't pick zero,
that's not allowed.
2:19
Lastly, if the music is done playing
we can stop running in the foreground.
2:23
Let's call stopForeground
right below stopSelf and
2:29
pass in true to make sure that
we remove the notification.
2:33
And that's it, that's all you need to do
to run your service in the foreground.
2:39
If we were making a more finished product,
2:44
we might want to add some buttons to our
notification and update our app icon.
2:46
But we're not, so
let's go ahead and test the app and
2:51
see what a foreground service looks like.
2:54
But before we get to testing,
3:00
it looks like there's an error
with this call to build.
3:02
It looks like this call
requires API 16 but
3:08
our current min is 15,
down here at the bottom left.
3:12
Let's open our app's
build.gradle file to fix this.
3:17
And set minSdkversion to 16,
and then sync now.
3:27
And if we go back to player service,
no more errors.
3:34
Let's get back to testing the app.
3:39
And if we play the music,
there's our notification.
3:48
And if we quit the activity,
the notification is still there.
3:54
And if we wait for the song to end,
4:00
we could even watch our notification
disappear because the service was stopped,
4:02
and it triggers the stop
foreground method.
4:07
Fantastic work.
4:09
This is definitely some of the most
difficult material in Android,
4:11
and you should be extremely proud for
finishing this course.
4:15
Keep practicing and I'll see you soon.
4:19
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up