Android activity and service communication

Source: Internet
Author: User

1. When acitivity and service are in the same application and process, they are implemented by inheriting the binder class.

WhenActivityBind toServiceIt is responsible for maintenanceServiceInstance reference, allowing youServiceCall some methods.For example, if you have a background service for playing background music, you can use this method for communication.

CodeAs follows:

 /**  * ********************* Service Code *************** ************************  */ 
Public Class LocalServiceExtends Service {
Private Final Ibinder binder = New Localbinder ();

Public Class Localbinder Extends Binder {
LocalService getservice (){
Return LocalService. This ;
}
}

Public Ibinder onbind (intent ){
Return Binder;
}
}

/** *********** ************************* */
Public Class Bindingactivity Extends Activity {
LocalService;

Private Serviceconnection mconnection = New Serviceconnection (){
Public Void Onserviceconnected (componentname classname, ibinder localbinder ){
LocalService = (localbinder) localbinder. getservice ();
}
Public Void Onservicedisconnected (componentname arg0 ){
LocalService = Null ;
}
};

Protected Void Onstart (){
Super . Onstart ();

Intent intent = New Intent ( This , LocalService. Class );
Bindservice (intent, mconnection, context. bind_auto_create );
}

Protected Void Onstop (){
Super . Onstop ();
Unbindservice (mconnection );
}

Public Void Printrandomnumber {
Int Num = LocalService. getrandomnumber ();
System. Out. println (Num );
}
}

Code explanation:

UseContext. Bindservice () will start the service:
Context. Bindservice ()-> oncreate ()-> onbind ()-> Service Running
Onunbind ()->Ondestroy ()-> service stop

activity can be bound to service interface onbind (). service and activity connections can be used serviceconnection , A new serviceconnection , rewrite onserviceconnected and onservicedisconnected method. bind the instance, call the bindservice method, input a service > intent (explicit or implicit) and a serviceconnection instance. once a connection is established, you can use the onbind () interface of service () Get servicebinder Instance to obtain Service Instance reference. Once Service Object found, You can get its public methods and attributes. However, this method must be in the same process and application.


Related Article

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.