In the last section we learned how to start a service with StartService, this section learns how to start a service with Bindservice
First look at the code section of myactivity:
public class MyActivity extends Activity {private button btn_start;private button btn_end;private myserviceconnection sCo nnection; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_myservice); Btn_start = (Button) Findviewbyid (R.id.button1); Btn_end = (Button) Findviewbyid (R.id.button2); Btn_start.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//start service Intent Intent = new Intent (myactivity.this, Myservice.class);//startservice (intent); LOG.I ("MyActivity", "Create service button is pressed!"); Bindservice (Intent, sconnection, context.bind_auto_create);}); Btn_end.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {//Destroy service Intent intent = new Int ENT (Myactivity.this, Myservice.class);//stopservice (intent); LOG.I ("MyActivity", "Destroy service button is pressed!"); Unbindservice (sconnection);}});} public class Myserviceconnection implements serviceconnection{@Override//when connectedAfter the service succeeds, call public void onserviceconnected (ComponentName arg0, IBinder arg1) {//TODO auto-generated method Stublog.i (" MyActivity "," onserviceconnected ");} Call public void onservicedisconnected (ComponentName arg0) {//TODO auto-generated @Override//When the process where the server is located is terminated due to an unexpected termination or other reason Method Stublog.i ("MyActivity", "onservicedisconnected");}}}This time we use Bindservice to start the service, and Bindservice and StartServer are obviously different from the parameters that are passed. Bindservice requires three parameters:
Parameter 1:intent don't say much.
Parameter 2: This parameter is an Serviceconnection object that listens to the connection between the visitor and the service. Where the onserviceconnected is called when the caller and the service are connected successfully, it is important to note that onservicedisconnected is not called when the caller is disconnected from the service, but is terminated for some reason.
Parameter 3: Specifies whether the service is automatically created when binding, of course, when the service is not yet created.
Layout file The same as the last time, no more say
The code for MyService is as follows:
public class MyService extends Service {private MyThread thread;private Boolean stopflag = false; @Override//methods that must be implemented public IBinder Onbind (Intent arg0) {//TODO auto-generated method stub//service after running, start thread if (!stopflag) {Thread.Start (); Thread.setflag (TRUE);} LOG.I ("MyService", "Onbind------------"); return null;} @Override//is created when calling public void OnCreate () {//TODO auto-generated method stub//When the service is created, instantiate mythreadthread = new MyThread (); LOG.I ("MyService", "OnCreate------------"); Super.oncreate ();} @Override @deprecated//onstart method is now replaced by ONSTARTCOMMD, in fact Onstartcommand also called Onstartpublic void OnStart (Intent Intent, int Startid) {//TODO auto-generated Method Stubsuper.onstart (Intent, Startid); LOG.I ("MyService", "OnStart------------");} @Override//Start call public int Onstartcommand (Intent Intent, int flags, int startid) {//TODO auto-generated method stublog.i ("MyService", "Onstartcommand------------");//After service runs, start thread if (!stopflag) {Thread.Start (); Thread.setflag (true);} Return Super.onstartcommand (Intent, flags, Startid);}@Overridepublic void OnDestroy () {//TODO auto-generated method stub//When the service is destroyed, stop the thread task Thread.setflag (FALSE); LOG.I ("MyService", "OnDestroy------------"); Super.ondestroy ();} @Overridepublic boolean onunbind (Intent Intent) {//TODO auto-generated method stublog.i ("MyService", " Onunbind------------"); return Super.onunbind (intent);} Class MyThread extends thread{//set flagpublic void Setflag (Boolean flag) {Stopflag = flag;} @Overridepublic void Run () {super.run (); while (Stopflag) {//sets the time output mode SimpleDateFormat format = new SimpleDateFormat (" Yyyy-mm-dd HH:mm:ss "); String time = Format.format (new Date ()),//Display LOG.I ("MyService"), try {//Delay one second thread.sleep (n);} catch ( Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Of course, remember to register the service in the configuration file
Next look at the results analysis:
When you click Start Service:
You can see that the service is already bound
When you click Destroy service:
You can see that the service has been unbound while the service is destroyed.
Today about the Bindservice launch service is here
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Four Component Learning Service three