Service of Android four components

Source: Internet
Author: User

The method by which the activity binds the service and invokes the service in the activity.

If a service object is not invoked directly in the activity, it is not possible to invoke the service, because this time the service has not started, this time needs to call the Bindservice method in activity, so that the activity and the service binding, after binding service, Automatically invokes the Onbind () method in the service, returning a binder object to the activity to invoke the method in service.

Onbind ()
This method is called by the system when the component calls Bindservice () to bind to the service (for example, to perform interprocess communication). In your implementation, you must provide a return IBinder to enable the client to use it to communicate with the service, and you must always implement this method, but if you do not allow binding, then you should return NULL.


I've never understood the principle of the Bindservice () method, let's write your own understanding:

1, in the service, create a new internal class Mybinder extends Binder, in this class to implement the communication method with the service. There is also a onbind () method in the service, which is executed only when the activity calls Bindservice () and returns a Binder object, the object of the inner class Mybinder created in the service.

2. When invoking the Bindservice object in activity, the newly created anonymous inner class, new Serviceconnect () {

Here Onserviceconnect (componentname name, IBinder Service) The service in this parameter is the object that we return in the service's Onbinder () method.

With this service object, you can do the service in a way.

This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1943315

Service of Android four components

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.