I. Introduction to binding services
As mentioned in the previous article, the service is generally enabled through startservice. When a visitor closes, the service still exists. However, if a visitor needs to communicate with the service, then we need to bind the visitor to the service; if context is used. when the bindservice () method starts the service, when the service is not created, the system will call the oncreate () method of the service and then call the onbind () method. Then, the visitor is bound to the service; if bindservice () is called multiple times to bind the service, no side effects (resulting in multiple bindings) will occur. The running process of binding the service is shown in:
Ii. Bind service core code
Binding and unbinding services
(1) Context. bindservice (intent service, serviceconnection Conn, bind_auto_create); // bind Service (2) Context. unbindservice (serviceconnection conn );
// Unbind the service
Serviceconnection
Serviceconnection is an interface used to bind and unbind ibinder. Therefore, you need to create a class to implement it; Class xxxserviceconnection implements serviceconnection {
@ Override
Public void onserviceconnected (componentname name, ibinder Service ){
// The service is the ibinder returned in onbind.
// Binder object
}
@ Override
Public void onservicedisconnected (componentname name ){
// Unbind the binder object
}
}
Service
Class xxxservice extends Service {private ibinder binder = new
Xxxbinder (); Public ibinder onbind (intent) {return binder;} public int fun (int ){
// Method provided by the Service, but cannot directly call...} private class xxxbinder extends binder implements ixxxbinder {
// Interface-oriented programming public return fun1 (int ){
// Apireturn fun (a) exposed externally );
// Method for calling the service internally }}}
Iii. bindservice case
Scenario: the service provides an addition operation interface. The activity needs to call the addition operation method provided by the Service and return the result;
Effect description:
Mainactivity. Java
Package COM. xiazdong. bindservice; import android. app. activity; import android. content. componentname; import android. content. intent; import android. content. serviceconnection; import android. OS. bundle; import android. OS. ibinder; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. textview; public class mainactivity extends activity {private edittext OP1, OP2; private button; private textview result; private iaddop binder; // use the iaddopprivate serviceconnection conn = new addopserviceconnection () interface (); private onclicklistener listener = new onclicklistener () {@ overridepublic void onclick (view v) {int number = binder. addopservice (integer. parseint (op1.gettext (). tostring (), integer. parseint (op1.gettext (). tostring (); result. settext ("result =" + number + "") ;};@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); OP1 = (edittext) This. findviewbyid (R. id. OP1); OP2 = (edittext) This. findviewbyid (R. id. OP2); Result = (textview) This. findviewbyid (R. id. result); button = (button) This. findviewbyid (R. id. button); button. setonclicklistener (listener); intent service = new intent (this, addopservice. class); this. bindservice (Service, Conn, bind_auto_create);} private class addopserviceconnection implements serviceconnection {@ overridepublic void onserviceconnected (componentname, ibinder Service) {// binder = (iaddop) service ;}@ overridepublic void onservicedisconnected (componentname name) {// call binder = NULL when unbinding a service ;}}}
Addopservice. Java
Package COM. xiazdong. bindservice; import android. app. service; import android. content. intent; import android. OS. binder; import android. OS. ibinder; public class addopservice extends Service {private ibinder binder = new addopbinder (); Public int add (int A, int B) {// return a + B Provided by the Service ;} @ overridepublic ibinder onbind (intent) {return binder;} private class addopbinder extends binder implements iaddop {public int addopservice (int A, int B) {// binder exposes an API call method provided by the Service Return add (a, B );}}}Iaddopservice. Java
package com.xiazdong.bindservice;public interface IAddOp {public int addOpService(int a,int b);}