Impact of mixed use of StartService and bindservice on service life cycle

Source: Internet
Author: User

FBI Warning: Welcome reprint, but please indicate source: http://blog.csdn.net/codezjx/article/details/45314925 , do not use for commercial purposes without my consent, thank you for your support!


Project development has encountered StartService and Bindservice mixed use of the situation, found that it has a great impact on the service life cycle, so share with you ...

first, the normal situation (It should be very familiar to everyone, Brief introduction):

(1) Use StartService alone ():

OnCreate ()->onstartcommand ()->service Running->ondestroy ()->service shut down

(2) Use Bindservice alone ():

OnCreate ()->onbind ()->clients is bound to Service->onunbind ()->ondestroy ()->service shut down

second, common use situation (Startservice->bindservice or Bindservice->startservice, order does not matter):

Example one: sequentially 1,2,3,4 execution

(1) Startservic: Call OnCreate ()->onstartcommand ()

(2) Bindservice: Call Onbind ()

(3) StopService: No call Ondestory () service is still running!

(4) Unbindservice: Call Onunbind ()->ondestory () The service is now off!

Example two: Replacing the example 13, 4

(1) Startservic: Call OnCreate ()->onstartcommand ()

(2) Bindservice: Call Onbind ()

(3) Unbindservice: Call Onunbind () service is still running!

(4) StopService: Call Ondestory () The service is not turned off at this time!

From the subtle changes above, we can conclude that the stop service and destruction service is two different concepts , although we call the StopService to stop the service, but the service still has to destroy the wood, still running firm. Until we call Onunbind, the Android system calls OnDestroy to destroy it after the client associated with the service is unbound.

Why? Let's take a look at the official explanation:

Note that if a stopped service still have serviceconnection objects bound to it with the bind_auto_create set, it would not is destroyed until all of these bindings is removed.

Summary: If the service that is stopped is still serviceconnection with its binding, then the service cannot be destroyed until we have all the serviceconnection untied

Accordingly, example two when we use Onunbind to unbind, the service still runs until the user calls Stopservice,service to destroy.

Why? Let's take a look at the official explanation:

When the last client unbinds from the service, the system destroys the service (unless the service is also started by STA Rtservice ()).

Summary: when all serviceconnection are unbound, the system automatically destroys the service. Note the contents of the brackets: Do not include the simultaneous use of StartService () to start the case. At this point, we have to call StopService again to destroy it.



To share a picture of the official, more intuitive:




For detailed analysis, please refer to the Official API documentation: http://developer.android.com/guide/components/bound-services.html



Impact of mixed use of StartService and bindservice on service life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.