This course will be retired on June 1, 2025.
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 go over the various return values from onStartCommand and what each of those values means!
Related Links
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
Enough of this stopping services stuff.
0:00
Let's get back to the par with
the invincible service refuses today.
0:02
But first a quick recap.
0:08
The return value of onStarCommand
represents what will happen to a service
0:10
if its process is killed after
returning from onStarCommand,
0:14
but before being stopped.
0:19
Put more simply,
0:21
it's what will happen if your service
is killed without finishing its work.
0:22
Earlier I mentioned that there
are three constants we can return from
0:28
onStarCommand.
0:31
Those constants are START_STICKY,
START_NOT_STICKY, and
0:32
START_REDELIVER_INTENT.
0:37
There's actually a fourth value
START_STICKY_COMPATIBILITY.
0:40
But it's not important to learn about,
so we've just got START_STICKY,
0:44
START_NOT_STICKY, and
START_REDELIVER_INTENT.
0:48
Let's kick things off with START_STICKY.
0:52
Returning Service.START_STICKY and
0:55
onStartCommand tells Android to restart
our service using a null intent.
0:57
This is useful for things you might
want to leave running for a long time.
1:03
Like a music player or
an app to track where you went on a run.
1:07
Once these things are started, it's up
to the user to decide when to stop them.
1:11
If I'm out running and Android kills
my run tracking app, I'm going to be
1:17
pretty unhappy if it doesn't come back and
track the rest of my run.
1:22
S is just what you think it would be.
1:25
If we return Service.START_NOT_STICKY
from onStartCommand,
1:29
then our service won't be restarted.
1:34
An example of this would be a service
that checks for new emails in your inbox.
1:36
And let say this service is
started every five minutes.
1:41
So every five minutes,
1:44
this service will run and
update your inbox with any new messages.
1:46
But if Android is so
burdened that it kills your app,
1:51
it's probably not the best idea to throw
rescheduling this service into the mix.
1:55
After all, it'll be started
again in five minutes anyway.
2:00
Last, but not least,
is START_REDELIVER_INTENT.
2:06
If we return
Service.START_REDELIVER_INTENT
2:09
from onStartCommand, our service will be
restarted using the last delivered intent.
2:12
In fact, for every intent,
2:18
if the associated start ID hasn't
been included in a call to stop self
2:20
then our service's onStartCommand
method will be called with the intent.
2:25
Here's an example.
2:30
Let's say we've downloaded the first two
songs and then our process gets killed.
2:32
Since we've only downloaded
two songs we haven't yet
2:35
called stopSelf for songs 3, 4 or 5.
2:40
So the three intense associated with
these songs will be redelivered to
2:43
our service when it's restarted.
2:48
And there we go.
2:51
Those are the three things we can have
happen when our process is killed
2:53
while our service is still working.
2:56
We can restart the service with
a null intent, START_STICKY,
2:59
not restart the service, START_NOT_STICKY,
or restart the service and
3:03
redeliver all unfinished intents,
START_REDELIVER_INTENT.
3:08
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