In many cases, the service requires more than just running in the background, communicating with the activity, or accepting the activity's command, how to implement it, and see the code.
Define a service
1 //creates a service and then returns an instance of the inner class (inherited from Binder) in Onbind (), enabling the activity to obtain the instance and manipulate the service2 Public classMyServiceextendsService {3 4 //creates an instance of an inner class that, when acquired by the activity, operates the service5 PrivateMbinder Binder =NewMbinder ();6 7 //Create an inner class that inherits from Binder, and functions that require activity are defined in this inner class8 classMbinderextendsBinder {9 Public voidFunction1 () {TenLOG.D ("Myservice.mbinder", "Function1 executed"); One } A - Public voidFunction2 () { -LOG.D ("Myservice.mbinder", "Function2 executed"); the } - - } - @Override + Public voidonCreate () { - Super. OnCreate (); +LOG.D ("MyService", "OnCreate executed"); A } at - @Override - Public intOnstartcommand (Intent Intent,intFlagsintStartid) { -LOG.D ("MyService", "Onstartcommand executed"); - return Super. Onstartcommand (Intent, flags, startid); - } in - @Override to Public voidOnDestroy () { + Super. OnDestroy (); -LOG.D ("MyService", "OnDestroy executed"); the } * $ @OverridePanax Notoginseng Publicibinder onbind (Intent Intent) { -LOG.D ("MyService", "Onbind executed"); the //This internal class instance is returned at bind time, and in the active onserviceconnected () function, this instance can be obtained + returnBinder; A } the +}
The main activity defines two buttons, which are used to bind and unbind activities, respectively.
1 Public classMainactivityextendsActivityImplementsOnclicklistener {2 PrivateButton bind;3 PrivateButton unbind;4 PrivateMyservice.mbinder Binder;5 6 PrivateServiceconnection connection =Newserviceconnection () {7 8 @Override9 //activities and services are automatically invoked when the bindings are lost, such as when a service is killed or an error is received, Serviceconnection will remain active and will receive a notification when the activity is next launchedTen Public voidonservicedisconnected (componentname name) { OneLOG.D ("Mainactivity", "onservicedisonnected executed"); A } - - @Override the //automatically called when activities and services are unbound, that is, when the Bindservice () function is called - Public voidonserviceconnected (componentname name, IBinder service) { -Binder = (myservice.mbinder) service;//get a Myservice.mbinder object with service down transformation -Binder. Function1 ();//execute functions inside the service, i.e. command service operations + Binder. Function2 (); -LOG.D ("Mainactivity", "onserviceconnected executed"); + } A }; at - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Bind =(Button) Findviewbyid (r.id.bt_bind); inUnbind =(Button) Findviewbyid (r.id.bt_unbind); -Bind.setonclicklistener ( This); toUnbind.setonclicklistener ( This); + } - the @Override * Public voidOnClick (View v) { $ Switch(V.getid ()) {Panax Notoginseng CaseR.id.bt_bind: -Intent Intent =NewIntent ( This, MyService.class); the //The intent of the incoming service, and the Serviceconnection object that is already defined, bind_auto_create as a flag that represents an instance of the service automatically created after the activity and service bindings + Bindservice (Intent, connection, bind_auto_create); A Break; the CaseR.id.bt_unbind: + // Unbind - Unbindservice (connection); $ Break; $ default: - Break; - } the } - Wuyi}
Register for this event in Androidmanifest.xml
<service android:name= ". MyService "></service>
Operation Result:
The principle of communication: When the binding operation is performed, the intent of the incoming service, the flag bit bind_auto_create, the service is created, and the Onbind () method is executed, the instance of the inner class is returned, and then the onserviceconnected () The method is executed, and the parameter of this method ibinder the instance that we return in the service's Onbind () method, and then we can get an instance of the service inner class after it is transformed down, and the instance can invoke the method in the class to achieve the effect of operating the service in the activity. After performing the unbind operation, the OnDestroy method of the service is executed first, and the onservicedisconnected () method does not execute, because this method is executed only when the service is killed or crash, and a report message is sent to the activity on the next binding.
Android Development Learning Path-service and activity communication