Reprinted from: http://www.cnblogs.com/onlylittlegod/archive/2011/05/15/2046652.html
Recently I learned about the service application in Android, and got stuck in the bindservice section, mainly because I didn't fully understand why it was implemented.
Bindservice and started service are both services. What is the difference:
1. the startservice () method is used in the started service to call the method. There is no connection between the caller and the service. Even if the caller exits, the Service continues to [oncreate ()-> onstartcommand () -> startservice ()-> ondestroy ()]. Note that there is no onstart (), which is mainly replaced by the onstartcommand () method. The onstart method is not recommended.
2. bindservice uses the bindservice () method to bind the service. When the caller and the biner are bound together, the consumer terminates the [oncreate ()-> onbind ()-> onunbind () -> ondestroy ()].
Caller activity:
Mainactivity
1 Mainacitvity 2 3 Package Com. zys. Service; 4 5 Import Com. zys. Service. bindservice. mybinder; 6 7 Import Android. R. bool; 8 Import Android. App. activity; 9 Import Android. content. componentname; 10 Import Android. content. context; 11 Import Android. content. intent; 12 Import Android. content. serviceconnection; 13 Import Android. OS. Bundle; 14 Import Android. OS. ibinder; 15 Import Android. View. view; 16 Import Android. View. View. onclicklistener; 17 Import Android. widget. Button; 18 19 Public Class Mainactivity Extends Activity { 20 Private Button startbtn; 21 Private Button stopbtn; 22 Private Boolean Flag; 23 /** Called when the activity is first created. */ 24 @ Override 25 Public Void Oncreate (bundle savedinstancestate ){ 26 Super . Oncreate (savedinstancestate ); 27 Setcontentview (R. layout. Main ); 28 Flag = False ; 29 // Set 30 Startbtn = (button) This . Findviewbyid (R. Id. startbtn ); 31 Stopbtn = (button) This . Findviewbyid (R. Id. stopbtn ); 32 Startbtn. setonclicklistener (listener ); 33 Stopbtn. setonclicklistener (listener ); 34 } 35 36 Private Onclicklistener listener = New Onclicklistener (){ 37 38 @ Override 39 Public Void Onclick (view v ){ 40 // Todo auto-generated method stub 41 Switch (V. GETID ()){ 42 Case R. Id. startbtn: 43 Bindservice (); 44 Break ; 45 Case R. Id. stopbtn: 46 Unbind (); 47 Break ; 48 Default : 49 Break ; 50 } 51 } 52 53 54 }; 55 56 Private Void Bindservice (){ 57 Intent intent =New Intent (mainactivity. This , Bindservice. Class ); 58 Bindservice (intent, Conn, context. bind_auto_create ); 59 } 60 61 Private Void Unbind (){ 62 If (Flag =True ){ 63 Unbindservice (conn ); 64 Flag = False ; 65 } 66 } 67 68 Private Serviceconnection conn = New Serviceconnection (){ 69 70 @ Override 71 Public Void Onservicedisconnected (componentname name ){ 72 // Todo auto-generated method stub 73 74 } 75 76 @ Override 77 Public Void Onserviceconnected (componentname name, ibinder Service ){ 78 // Todo auto-generated method stub 79 Mybinder binder = (Mybinder) service; 80 Bindservice = Binder. getservice (); 81 Bindservice. mymethod (); 82 Flag = True ; 83 } 84 }; 85 86 }
Service bindservice:
Bindservice
Bindservice Package Com. zys. Service; Import Java. Io. filedescriptor; Import Android. App. Service; Import Android. content. intent; Import Android. OS. Binder; Import Android. OS. ibinder; Import Android. OS. iinterface; Import Android. OS. parcel; Import Android. OS. RemoteException; Import Android. util. log; Public Class Bindservice Extends Service { Private Static Final String tag = "bindservice" ; Public Void Mymethod () {log. I (tag, "Bindservice --> mymethod ()" ) ;}@ Override Public Ibinder onbind (intent ){ // Todo auto-generated method stub Return Mybinder ;} Public Class Mybinder Extends Binder { Public Bindservice getservice (){ Return Bindservice. This ;}} Private Mybinder =New Mybinder ();}
Because the service in Android uses the onbind method to bind the service, an ibinder object is returned for operation. When we want to obtain the content of the specific service method, we need to return a specific service object for the ibinder object to operate. Therefore, the specific service object must first implement the binder object. In this way, we can use the bindservice method to bind the service, after obtaining the binder object, obtain the specific service object, and then obtain the methods in the service. Therefore, we need to note that the bindservice method to bind the Service must implement the binder object, therefore, we must use the binder method to obtain the service instead of directly using the service class. This is restricted by the internal implementation of Android.
The procedure is as follows:
Intent intent = new intent (mainactivity. This, bindservice. Class)-> New bindservice object-> New mybinder object
-> Bindservice (intent, Conn, context. bind_auto_create);-> onbind () function ----- pass the mybinder object -------> onserviceconnected () --> get the bindservice object corresponding to the binder object through the passed binder object --> call the method defined in service.
This must be passed through the binder object, because it is passed through the binder object, get the service object through the binder object, and then get the required service, so the Service must implement the binder for transmission and use.