What are START_STICKY, START_NOT_STICKY and START_REDELIVER_INTENT Service

I can not understand

  • START_STICKY
  • START_NOT_STICKY and
  • START_REDELIVER_INTENT

can anyone clearly explain the examples.

I went through this link, but could not understand it clearly.

early.

+43
android android-intent
Dec 27 '12 at 12:19
source share
3 answers

They are related to services. We all know that services continue to run in the background, and they also consume some memory to execute.

So, since more applications are running on the Android device, the device’s memory continues to decline, and when the time comes when the device’s memory becomes critically low, the android system begins to terminate the processes to free up the occupied memory by the processes.

But you can perform some important tasks with services, which can also be terminated as the service stops. therefore, these concepts should tell the Android system what action you want to perform when the device’s memory is stabilized and when it is ready to restart services.

The simplest explanation for this might be

START_STICKY- informs the system about creating a new copy of the service when a sufficient amount of memory is available, after it is restored from low memory. Here you will lose results that could be calculated earlier.

START_NOT_STICKY- tells the system not to bother restarting the service, even if it has enough memory.

START_REDELIVER_INTENT- tells the system to restart the service after the failure, and also to renew the intentions that were present during the failure.

+87
Dec 27 '12 at 12:35
source share

Well, I read the thread in your link and she says it all.

if your service was killed by Android due to low memory, and Android clears some memory, then ...

  • STICKY: ... Android will restart your service because this special flag is set.
  • NOT_STICKY: ... Android will not care to start again, because the flag tells Android that it should not bother.
  • REDELIVER_INTENT: ... Android will restart the service and resubmit the same intention to the onStartCommand() service, because the flag is again.
+3
Dec 27 '12 at 12:35
source share

Both codes are applicable only if the phone runs out of memory and it kills the service before it finishes executing. START_STICKY tells the OS to recreate the service after it has enough memory, and call onStartCommand () again with zero intent. START_NOT_STICKY tells the operating system not to worry about re-creating the service. There is also a third code, START_REDELIVER_INTENT, which tells the OS to recreate the service and re-deliver the same intention to onStartCommand ().

This Dianne Hackborn article explained this much better than the official documentation.

Source: http://android-developers.blogspot.com.au/2010/02/service-api-changes-starting-with.html

The key part here is the new result code returned by the function, telling the system what it should do with the service if its process is killed during its launch:

START_STICKY basically coincides with the previous behavior, the service remains "started" and will later be restarted by the system. The only difference from previous versions of the platform is that if it restarts because its process is killed, onStartCommand () will be called in the next instance of the service with zero intent instead of not being called at all. Services that use this mode should always check this case and treat it accordingly.

START_NOT_STICKY says that after returning from onStartCreated (), if the process is destroyed without the remaining start commands for delivery, the service will be stopped instead of restarting. This makes more sense for services that are designed only to run the execution of commands sent to them. For example, a service can be started every 15 minutes from an alarm to poll some network status. If he killed while doing this job, it would be best to let him stop and start the next time the alarm goes off.

START_REDELIVER_INTENT is similar to START_NOT_STICKY, unless the service process is killed before it calls stopSelf () for a given intention, that intention will be passed to it until it completes (if after a number of other attempts it still does not may be completed, indicating that the system is failing). This is useful for services that receive work commands and do so ultimately shut down for each team sent.

+2
Nov 12 '13 at 16:10
source share



All Articles