This video explains Android Foreground Services, a crucial component for apps that need to perform tasks in the background while the user is aware of the ongoing operation, primarily through persistent notifications.
Mind Map
クリックして展開
クリックしてインタラクティブなマインドマップを確認
hey guys and welcome back to a new video
and a new episode of Android basics in
this video I will talk about services
but foreground Services specifically
what that is when you should use these
and most importantly how you should use
these so till now in this Android Basics
playlist We only covered use cases where
our app is actually active where the
user seees something can click some
buttons or maybe also receive some
events from other apps so broadcast what
I what I talked about in the last video
but those were really only to to handle
onetime events very quickly but what if
your app actually needs to run in the
background for example you have a music
player app then the user would normally
expect that your app also plays music
when it's minimized or you have a
running Tracker app that tracks wherever
you you want to have a run then you also
expect that the tracking doesn't stop
when you minimize the app and that's
where so-called Services come into play
a service is an Android component just
like an activity but with a difference
that it doesn't have a user interface
but runs in the background and there an
different types of such services on the
one hand we have General Normal services
but on the other hand as this video is
called we also have the so-called
foreground Services the difference is
that with normal Services the user is
not aware that the service is running
and that your app is doing something in
the background and with foreground
Services the user knows that so for
example if your app just synchronizes
some data periodically it doesn't need
to do that in a foreground service
because the user does not need to be
aware that your app is currently syncing
something on the other hand if your app
is playing music music then the user
should be aware that your app is
actually doing something in the
background by seeing a notification
where the user can also control what the
service is currently doing so they can
pause the music they can skip to the
next song these kinds of things and the
problem now with these normal Services
where the user is not aware that these
run is that they simply can't be
guaranteed to run indefinitely cuz if
the Andro system needs memory it might
just decide to kill your service with
foreground services on the other hand
and that can't happen because they
require that as long as the service runs
the user sees a notification so for
example hey you're curbly having an
active run and this is your time because
the N system then assumes that the user
wants the service to stay active it
won't kill it even if it's low on memory
and that is why nowadays we mainly use
foreground Services when it comes to
using Services because for these
services that just sync some data in the
background that executes some longer
running tasks in the background we have
something called work manager which I
talk about in the next video so it's
just rather rare that you use these
normal General Services there are still
use cases but usually if you use a
service it's a foreground service
nowadays and to get started with that
let's just dive into coding and create a
service class in our root package new
class called yeah let's just call it
running service and we simil a little
running Tracker app here just super
simple with a static information select
class hit enter and here we want to
inherit from service so even though we
are creating a foreground service it
just needs to inherit from service and
inside of the service itself we now
specify what kind of service that is you
can see we need to implement a function
here which is called onbind which
receives an intent and returns an i
binder what is that that function is
used to create something called a bound
service so what you can also do with a
service is you can have one active
instance and multiple components connect
to that single instance and have done
yeah a stream for for communic to
communicate with that service and also
receive such events um or callbacks back
from that service if that is a use case
so if multiple apps need to connect to
one single service for example then you
need this then you also can't use
something like work manager as I will
cover in the next video but we want to
keep it simple here and we just want to
return not so we just say nothing can
actually bind to the service I will show
you different ways of communicating with
a service from an activity but let's
let's keep it simple here so right now
the service obviously doesn't do anything
anything
let's change that let's first of all
Define different commands that our
activity or other Android components can
send to the service on the one hand we
want to decide when we launch the
service or when we start it and want to
decide when we stop it for example if
the run is finished and we do this by
overwriting the onart command function
this is triggered whenever another
Android component sends an intent to
this running service so again intents
are the way how the communication works
or how intentions are specified and send
to the service and this intent can of
course contain data so depending on what
we want to do here we can just use a
different action in our intent so let's
just go down here and specify an Edom
class for the different actions we can
have for this running service and in
this simple example it's just start and
stop but you would potentially have a
lot of these actions depending on what
your service needs to do and then here
on start command before we return we
want to check when the intent
action is now actions. start. two string
then we want to call a function to start
or service which we'll create in a
moment and if it's actions. stop. two
string we want to stop our service in
this case it's very simple to stop
because we don't even need to free up
any resources or so so we don't need to
create a function for that separately we
can just call stop self which will stop
this Services instance starring on the
other hand is a bit more complex so we
need to do something there so let's go
down here and create a private function
to start our service and as I said if we
want to make this foreground service it
needs to come with a persistent
notification because otherwise the user
wouldn't be aware that there's a service
running or that the app isn't doing
something in the background so what we
need to do is we need to call start
foreground and this takes an ID and a
notification this IDE corresponds to the
specific notification so if you want to
update the notification you would just
call this function again with with the
same ID with the updated notification
but let's do it step by step we want to
choose one as the ID here don't choose
zero I've made that mistake before it
won't work it needs to be one at least
and now creating notification is
something we better do up here so we
create that Val notification is equal to notification