We want to bind the service to implement Serviceconnection this interface, this interface has two methods
@Override public void onserviceconnected (componentname arg0, IBinder service) {
Called when the service is successfully bound
}
@Override public void onservicedisconnected (ComponentName arg0) {
Called when the service is in the process of crashing, or when it kills.
}
We're going to pass the data through the IBinder object inside the onserviceconnected,
This object accesses the return value of IBinder () in the service, so we're going to create a new class to inherit from the system binder, and then let IBinder () return to this class,
At this time we can access the new class;
PackageCom.example.service_senddata;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.Binder;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder; Public classMyServiceextendsService {PrivateString data = "This is the default information"; Private BooleanFlag =false; @Override Publicibinder onbind (Intent Intent) {//TODO auto-generated Method Stub//returns the newly created class return NewObinder (); } //This class is used to inherit the binder class under the system, Public classObinderextendsAndroid.os.Binder {//The method of setting data below this class voidsetData (String data) {MyService. This. data =data; }} @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) { //TODO auto-generated Method StubBundle B =Intent.getextras (); Data= B.getstring ("msg")); return Super. Onstartcommand (Intent, flags, Startid); } @Override Public voidonCreate () {Super. OnCreate (); Flag=true; NewThread () { Public voidrun () { while(flag) {System.out.println ("Data obtained in service:" +data); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } }; }.start (); }; @Override Public voidOnDestroy () {//TODO auto-generated Method StubFlag =false; }}
Activity Code
PackageCom.example.service_senddata;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;Importandroid.app.Activity;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText; Public classMainactivityextendsActivityImplementsOnclicklistener, serviceconnection{PrivateButton Btn_startservice; PrivateButton Btn_stopservice; PrivateEditText Edt_data; //declaring an internal class for a service PrivateMyservice.obinder Binder =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Btn_startservice=(Button) Findviewbyid (R.id.btn_startservice); Btn_stopservice=(Button) Findviewbyid (R.id.btn_stopservice); Edt_data=(EditText) Findviewbyid (r.id.edt_data); Btn_startservice.setonclicklistener ( This); Btn_stopservice.setonclicklistener ( This); Findviewbyid (R.id.btn_bindservice). Setonclicklistener ( This); Findviewbyid (R.id.btn_unbindservice). Setonclicklistener ( This); Findviewbyid (R.id.btn_sycservice). Setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.btn_startservice:intent i=NewIntent ( This, MyService.class); I.putextra ("MSG", Edt_data.gettext (). toString ()); StartService (i); Break; CaseR.id.btn_stopservice:stopservice (NewIntent ( This, MyService.class)); Break; CaseR.id.btn_bindservice:bindservice (NewIntent ( This, MyService.class), This, context.bind_auto_create); Break; CaseR.id.btn_unbindservice:unbindservice ( This); Break; CaseR.id.btn_sycservice:if(binder!=NULL){ //calling methods within the inner classBinder.setdata (Edt_data.gettext (). toString ()); } Break; }} @Override Public voidonserviceconnected (componentname arg0, IBinder service) {//the Binder object here can access the return value of the Onbind () method under MyService//to initializeBinder =(Com.example.service_senddata. Myservice.obinder) Service; } @Override Public voidonservicedisconnected (componentname arg0) {//TODO auto-generated Method Stub } }
Service Code
Binding Service for communication