"Learning Notes" service communication in Android

Source: Internet
Author: User

first, service life cycle (onCreate onstarT ondestROy onbind)

1). The life cycle of the service being started:If an SERvice is called by an activity Context.staRtSeRvice method is started, then no matter if there is activity using BindseRvice binding or UnbindseRvice unbind to the SERvice, the SERvice are running in the background. If an SERvice was STARtSeRThe vice method starts multiple times, so OncRthe Eate method is only called once, OnstaRT will be called multiple times (corresponding to call STARtSeRvice), and the system will only create SERan instance of Vice (so you should know that you only need one stopseRvice Call). The SERvice will always run in the background, regardless of whether the program's activity is running until it is called StopseRvice, or its own Stopself method. Of course, if system resources are low, andRThe OID system may also end the service.

2). The life cycle of the service being bound:If an SERvice is called by an activity Context.bindseRthe vice method binding starts, regardless of the call BindseRvice called several times, OnCRthe Eate method is only called once, and OnstaRT method is always not called . When the connection is established, the SeRvice will run until Context.unbindse is called.Rvice disconnected or previously called BindseRThe vice Context does not exist (such as when the activity is finish) and the system will automatically stop the SERvice, corresponding OndestROy will be called.

3). The life cycle of a service that is started and bound:If an SERvice is also started and bound, the SERvice will always be running in the background. And no matter how it is called, OnCREate is always called only once, corresponding to STARtSeRHow many times vice calls, SeRVice's OnstaRHow many times T will be called. Calling UnbindseRvice will not stop SErvice , and you must call StopseRvice or SeRvice Stopself to stop the service.

4). Clear the service when the service is stopped:when an SERvice is terminated (1, call StopseRvice;2, call stopself;3, no longer have a bound connection (not started), OndestRThe Oy method will be called, where you should do some cleanup work, such as stopping in SERthe thread created and run in Vice.


Ii. External and service communications

In Mainactivity, get the data into the intent and start the service

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/4B/wKiom1gPAfKhmW06AAAQPBdzp5I265.png "title=" Qq20161025145510.png "alt=" Wkiom1gpafkhmw06aaaqpbdzp5i265.png "/>


How do I get data from mainactivity in the service? Since the service is started, the Onstartcommand method is executed, so we override this method to receive the data with intent

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/89/4B/wKiom1gPApWj8sVCAAAVwAi9Rb8061.png "title=" Qq20161025145814.png "alt=" Wkiom1gpapwj8svcaaavwai9rb8061.png "/>

Third, bind service communication (Bindserivce->oncreate->onbind->running->onunbind->ondestroy)

1) Execute bindings through Bindservice in mainactivity

Bindservice (New Intent (This,myservice.class), this, context.bind_auto_create);

2) Execute this function when the OnCreate function in MyService executes, and then onbind the function, overriding the method. The method returns a Ibinder,ibander is an interface, the method to return an instance, we need to write a class inherits the Android system Binder class

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/89/4F/wKiom1gPLXPx0uK9AAAY1EOmBzw563.png "title=" Qq20161025173713.png "alt=" Wkiom1gplxpx0uk9aaay1eombzw563.png "/>

3) The Ibander instance is called by the Mainactivity Bindservice's This call onserviceconnected function to receive

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/4E/wKioL1gPNSGBxeSDAAAdBg-gFqE745.png "title=" Qq20161025183352.png "alt=" Wkiol1gpnsgbxesdaaadbg-gfqe745.png "/>

4) Binder in mainactivity to call MyService in SetData method to transfer data, complete and service communication

Binder.setdata (Edt.gettext (). toString ());

"Learning Notes" service communication in Android

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.