Services in Android – Part 2, All about sticky and non sticky services

Services in Android – Part 2, All about sticky and non sticky services

hi guys Codetutor here and my name is Anil Deshpande, In this particular video I want to talk about the behavior of the
service. In the previous video I had shown that the onstart command method
returns a integer variable, so to understand this let us try to see what
happens to the app when a resource crunch situation arises. Assume that i
have a app that is running in this case it is a MyGroceryList, then I start
another app and as i keep on starting multiple apps there may arise a
situation where in resource crunch situation will happen and android may
actually decide to kill my app that is not running in the foreground. In case i
had a service that was running in my app which is running in the background then
android will not try to kill that particular app because Service is always
gets a higher priority but that doesn’t mean just because you keep a service it will not get killed if the resource
crunch actually do happen and it is very severe the android operating system may
actually decide to even kill the app that has a service. To understand the
service behavior let us understand what happens to a
service when a resource crunch happens in this particular graph you can see
that there will arise a situation where the android operating system has to
intervene and it needs to kill the service and later point in time as the
resources become available there will be a time where android
operating system will have enough resources to host the service that had
been killed earlier now the question is what should happen
to the service that had been killed that is determined by the value that you
have returned in the onstart command whether you want to restart the service
and whether the restarted service will have a intent value populated so that it
can continue with its work if you return the integer constant start sticky the
auto-start of the service is guaranteed the service will always automatically
get re-started when the resources will become available but it will get a null
intent then you have a start not sticky in this case the service will
not be automatically restarted that doesn’t mean the service will not
get started at all if you trigger an intent to start that service which had
been killed it will once again get started and then
you have another constant variable which is called as start redeliver intent
what happens in start redeliver intent is the service will always get restarted
and it will always get the intent which it had received in earlier case before
getting killed Let us now spend some time in
understanding the scenarios in which you would use these constants in case of
start sticky you use this in case of services which are being explicitly
managed that is it is a long-running service you are starting your service and you
stopping your service explicitly using . start service and stop service methods which i
have explained in the previous video The advantage of this particular start
sticky constant is if the service gets killed it will automatically get me
started but when it gets started, it will
not to get the intent values so the intent value will be null it doesn’t
remember what was the previous calculated value in the intent. The typical scenario is music service a music service which is constantly
running a music audio when it gets killed it will restart when the resources are
available but with the null intet so that it may not have to remember which song
was playing earlier you don’t want your music service to automatically start blasting music in your ears in your unexpected time
because it got killed some quiet time back in case of start not sticky you are not restarting the service but
if the intent is triggered you will start it. The scenario is
services are being run periodically and they self stop If service gets before self stopping
it will not be restarted automatically but after it getting killed, if an intent gets triggered then it will be started the best example
would be alarm service are kind of service which is periodically every few
minutes or every few seconds to do some kind of backend job, like polling
the data from the server every ten minutes or 20 minutes so during that time if we gets killed
there is no need to restart it because it is always going to be once again triggered after 10 or 20 minutes so tasks
which need to be run in the background periodically are best
examples of start not sticky services then comes the start redeliver intent
in this case what is happening is the service is always going to get restarted
and it will get the previous intent the typical scenario when you would use start
redeliver constant is the service is being explicitly managed
and it’s a long-running service if it gets killed you want it to always get
restarted and continue from where it had stopped a typical example is file
download if file download service gets killed somewhere in between because of
the resource crunch and when later resources become available the file download should continue from
there it had been stopped you don’t want it to once again
explicitly be started which is the case of start sticky are not started at all
which is the case with start not sticky so start reliever intent will make sure
that it will remember the intent with which it had been triggered and it will
continue from where it had stopped so that is start redeliver intent. I hope
this clarifies when to use start sticky start not sticky and start redeliver
intent constants in the onstart common method of the service stay tuned share comment and subscribe
to the channel bye

16 thoughts on “Services in Android – Part 2, All about sticky and non sticky services”

  1. Nice to listen to ur session. I attended ur live session in Mangalore. Really, its great. u r very knowledgeable. Keep posting …

  2. Dear sir,
    Nice video tutorial, I used the START_REDELIVER_INTENT (before seeing this tutorial) looking for a solution in fixing an issue with notifications which we are sending to our users (every 20 minutes interval — during 9 AM to 9 PM). This background service is started to failing in latest Chinese mobiles like Vivo, Oppo and Xiaomi, as these guys kill the application background tasks, as soon as the use presses back button. Tried alarm manager as well, but no luck. can you please suggest how to deal with this?

  3. In the previous video, u just told that there is also another method that we can use explicitly to stop the services but you don't have a word on how to return integer value on click of on start service as you said at the beginning of this video. I think there is a mismatch between the two videos. Kindly do the needful action. Thanks

  4. PostResponseAsyncTask task = new PostResponseAsyncTask(SampleService.this, data, (AsyncResponse) this);


    How to use this method in services

  5. i'm in the problem sir how to manage notifications and messages in rest api when application is killed. and notify the user

Leave a Reply

Your email address will not be published. Required fields are marked *