Android four basic components--service life cycle Detailed

Source: Internet
Author: User

Android four basic components--serviceservice

Life cycle Detailed


1. Service life cycle:


I> in the case of a non-binding service, only OnCreate (), Onstartcommand (), Ondestory () method:

operation method corresponds to life cycle one:

A, " executive StartService (Intent)"

execution Life cycle method:oncreate ()--->onstartcommand ();

B, " Executive StopService (Intent)"

execution Life cycle method:ondestory ();


operation method corresponds to life cycle two:

A, " executive StartService (intnet)"

execution Life cycle method:oncreate ()--->onstartcommand ();

B, " Execute return key "---> " execute StartService (intnet)"

execution Life cycle method:Onstartcommand ();

C, " Executive StopService (Intent)"

execution Life cycle method:ondestory ();

D, " executive StartService (intnet)"

execution Life cycle method:oncreate ()--->onstartcommand ();

E, " execute StopService (Intent)"---> " execute Return key "---> " execute StartService (intnet)" Execution life cycle method: oncreate ()--->onstartcommand ()

Summary:

< EM style= "Background-color:inherit;" >


Ii> in the case of binding service, only OnCreate (), Onbind (Intent Intent), Onunbind (Intent Intent), OnDestroy () method in case:


The operating method corresponds to the life cycle:

A, " executive Bindservice (Intent,conn, service.bind_auto_create)"

execution Life cycle method:oncreate ()--->onbind ()---> serviceconnection class onserviceconnected () ( Function: The method by which the activity and service establish a connection);

B, " Executive Unbindservice (conn)"

execution Life cycle method:onunbind ()--->ondestory ();


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/89/C4/wKiom1gb9faD_DeNAAA_NBiAmp0899.png "style=" float: none; "title=" 1.png "alt=" Wkiom1gb9fad_denaaa_nbiamp0899.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/89/C1/wKioL1gb9fazSqZTAAA2SQg7oYQ507.png "style=" float: none; "title=" 2.png "alt=" Wkiol1gb9fazsqztaaa2sqg7oyq507.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/89/C1/wKioL1gb9faz32vIAABF_mdumLs242.png "style=" float: none; "title=" 3.png "alt=" Wkiol1gb9faz32viaabf_mdumls242.png "/>



Iii> binding service and non-binding service mixed use life cycle of execution


Operation Method:

A, " Executive StartService"

execution Life cycle method:onCreate ()-->onstartcommand ();

B, " Executive Bindservice"

execution Life cycle method:onbind ()--->onserviceconnected ();

C, " Executive Onunbindservice"

execution Life cycle method:onunbind ();

D, " Executive Bindservice"

execution Life cycle method:onserviceconnected ()-->onrebind ();

E, " Executive Onunbindservice"

execution Life cycle method:onunbind;

F, " Executive StopService"

execution Life cycle method:ondestory ()


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/89/C4/wKiom1gb9lCxmcaXAABgDsGmlac359.png "title=" 4.png " alt= "Wkiom1gb9lcxmcaxaabgdsgmlac359.png"/>


Call service as follows:


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/C1/wKioL1gb9pWSge5RAACUo_Kt2GE739.png "title=" 5.png " alt= "Wkiol1gb9pwsge5raacuo_kt2ge739.png"/>

The following print results are performed:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/89/C1/wKioL1gb9uDDNxQMAABIuCdNKRw029.png "title=" 6.png " alt= "Wkiol1gb9uddnxqmaabiucdnkrw029.png"/>


Summarize:

1, when the service performs startservice operation, the execution of Bindservice, the service is bound after the case is not unbound, to execute StopService, the service Ondestory method will not be triggered at this time, However, after unbinding, continuing to execute StopService will trigger the Ondestory method.

2, in the Serviceconnection class, if the service unexpectedly breaks, then the Onservicedisconnected method in this class will be called to execute; normal interrupts (such as executing the Unbindservice method), The Onservicedisconnected method in this class is not called.

3. If a service is turned on using two modes (Bindservice and StartService), you must ensure that the two lifecycle is complete before the Ondestory method can be executed. For example: bindxx--"startxx--" Stopxx or startxx--"bindxx--" stopxx, neither of the above will implement the Ondestory () method.

4. Service and activity use Bindservice to bind, and the service's life cycle cannot be bound to the activity's life cycle. In some cases, unbinding is simply a matter of canceling the relationship between service and activity, and cannot stop the service.

5. The service is not able to perform time-consuming operations because the service is in the same process as the application and the service is not a new thread, and the ANR (application not responding) exception will occur if a time-consuming operation is performed.



2, Intentservice implementation principle


A> uses queues to manage intent requests, and whenever the client code initiates intentservice through a intent request, Intentservice joins the intent into the queue and then opens a new thread to handle the intent;


B> for asynchronous StartService () requests, intentservice processes the intent in the queue one at a time, guaranteeing that only one intent is processed at the same time;


C>intentservice creates a separate thread to handle the code implemented by the Onhandleintent () method, so developers don't have to deal with multithreading issues;


D> Intentservice stops automatically when a message request is processed and the developer does not need to call the Stopself () method to stop the service;


E> provides the default implementation for the service's Onbind () method, and the default implementation of Onbind () returns null;


The f> Onstartcommand () method for the service also provides a default implementation that adds the request intent to the queue.


3. The main difference between service and intentserviceactivity

INtentservice can perform time-consuming operations without causing the UI thread to block.



Summarize:

The above article through the operation in the Android studio, the complete display of the service common life cycle method of implementation process, through understanding the above process can facilitate our program development, in the appropriate life cycle, the implementation of the corresponding method, so as to help the program optimization, Convenient later maintenance and reduce unnecessary waste of memory, it is more helpful for us to learn the underlying Android. The service and Intentservice are also compared, and the principle of intentservice is implemented. Shortcomings, please do not hesitate to enlighten us.



This article is from the "in the Eyes of the Sun" blog, please be sure to keep this source http://wang963825.blog.51cto.com/8695943/1869281

Android four basic components--service life cycle Detailed

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.