This routine has three buttons Bindbutton, Unbindbutton, Getservicestatusbutton
Bind service, unbind, get service state, respectively
Bindservicetest.java binds the service to the activity and gets the running state of the service
public class Bindservicetest extends Activity {Button bindbutton,unbindbutton,getservicestatusbutton;// Maintains the IBinder object of the service being started Bindservice.mybinder binder;//defines a Serviceconnection object private Serviceconnection conn = new Serviceconnection () {@Override//callback the method when the activity and service connection is successful public void onserviceconnected (componentname name, IBinder service) {System.out.println ("--service connected--"); binder = (bindservice.mybinder) service;} Callback this method when the activity disconnects from the service @overridepublic void onservicedisconnected (componentname name) {System.out.println (" --service disconnected--");}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); Bindbutton = (Button) Findviewbyid (r.id.bind); Unbindbutton = (Button) Findviewbyid (R.id.unbind); Getservicestatusbutton = (Button) Findviewbyid (r.id.getservicestatus); final Intent Intent = new Intent (); Intent.setaction ("Crazyit.service.BIND_SERVICE"); Bindbutton.setonclicklistener (New Onclicklistener ({@Overridepublic void OnClick (View v) {Bindservice (intent, Conn, service.bind_auto_create);}}); Unbindbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {unbindservice (conn);}}); Getservicestatusbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) { Toast.maketext (bindservicetest.this, "Service count value is" + binder.getcount (), Toast.length_short). Show ();}});}}
Bindservice.java inherits the subclass of the service, implements the Onbind () method, and lets the method return a IBinder object that can access the service state data that will be passed to the visitor of the service
public class Bindservice extends Service {private int count;private Boolean quit;// Defines the object returned by the Onbinder method private Mybinder binder = new Mybinder ();//Implement IBinder class public class Mybinder extends by inheriting binder public int GetCount () {//Get service Running status: Countreturn count;}} You must implement a method that binds the service when the callback method @overridepublic IBinder onbind (Intent Intent) {System.out.println ("Service is binded");// Return IBinder object return binder;} The method is invoked when the service is created. @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate (); System.out.println ("Service is Created"), new Thread () {@Overridepublic void run () {while (!quit) {try{thread.sleep (1000 );} catch (Interruptedexception e) {e.printstacktrace ();} count++;}}}. Start ();} Callback this method when the Service is disconnected @overridepublic boolean onunbind (Intent Intent) {System.out.println ("service is onunbinded"); return true;} @Overridepublic void OnDestroy () {Super.ondestroy (); this.quit = true; System.out.println ("Service is destroyed");}}
Manifest file
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Crazyit.bindservice "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbackup=" tr UE "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/appth EME "> <activity android:name=" crazyit.bindservice.BindServiceTest "android:label=" @str Ing/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/& Gt <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity& Gt <!--Configure a service component--><service android:name= ". Bindservice "><intent-filter><!--for the Intent-filter configuration of the service component ACTIOn--><action android:name= "Crazyit.service.BIND_SERVICE"/></intent-filter></service> </ Application></manifest>
Bind service and communicate with it