1 Introduction
Service is one of Android's four components. Unlike activity, however, services do not interact directly with the user, but are run in the background in a touch.
There are two ways to start system services.
One is to call Context.startservice () and 方法。 the other is to call the Context.bindservice () method.
2 Life cycle of service
The following diagram illustrates the life cycle of a service. When the user calls Context.startservice () or Context.binservice (), the service is started.
If the service is not created, the system first calls the OnCreate () method in the service class, and then the OnStart () method or the Onbind () method executes, opening a service.
The service will be silently in the background, calling the Context.stopservice () or Stopself () method, and the service's OnDestroy () will execute and destroy the service.
When the service is opened through Context.bindservice (), the service must be stopped through Context.unbindservice ().
3 Example
3.1 Starting the service via Context.startservice ()
First create a new class that inherits the service class.
Public classMyServiceextendsservice{@Override Publicibinder onbind (Intent Intent) {return NULL; } @Override Public voidonCreate () {Super. OnCreate (); LOG.D ("MyService", "OnCreate executed!"); } @Override Public voidOnStart (Intent Intent,intStartid) { Super. OnStart (); LOG.D ("MyService", "OnStart executed!"); } @Override Public voidOnDestroy () {Super. OnDestroy (); LOG.D ("MyService", "OnDestroy executed!"); } }
The activity is then started or stopped by clicking the button in the activity.
3.1 Starting the service via Context.bindservice ()
Above, we can only start and stop the service through the buttons of the active interface. If the service wants to communicate more with the activity, there is another way to start the activity.
1, starting the service through Context.bindservice (), we also need to inherit the services class, overriding the Onbind () method.
In the Onbind () method, we return a binder object. The Binder class API is defined as follows:
Here is the specific code:
Public classMyServiceextendsService {PrivateDownloadbinder Mbinder =NewDownloadbinder (); DownloadbinderextendsBinder { Public voidstartdownload () {LOG.D ("MyService", "Startdownload executed"); } Public intgetprogress () {LOG.D ("MyService", "Getprogress executed"); return0; }} @Override Publicibinder onbind (Intent Intent) {returnMbinder; } ...}
You can see that we have defined an inner class downloadbinder inherited from Binder, and in this class we implemented two methods for starting downloads and getting progress.
2. The next step is to start the service through Context.bindservice () in the activity and interact with the service at any time using the method defined in the Downloadbinder above.
Here is the specific code
Public classMainactivityextendsActivityImplementsOnclicklistener {PrivateButton Bindservice; PrivateButton Unbindservice; PrivateMyservice.downloadbinder Downloadbinder; PrivateServiceconnection connection =Newserviceconnection () {@Override Public voidonservicedisconnected (componentname name) {} @Override Public voidonserviceconnected (componentname name, IBinder service) {Downloadbinder=(myservice.downloadbinder) service; Downloadbinder.startdownload (); Downloadbinder.getprogress (); } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ... bindservice=(Button) Findviewbyid (R.id.bind_service); Unbindservice=(Button) Findviewbyid (R.id.unbind_service); Bindservice.setonclicklistener ( This); Unbindservice.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.bind_service:intent bindintent=NewIntent ( This, MyService.class); Bindservice (bindintent, Connection, bind_auto_create); //Binding Service Break; CaseR.id.unbind_service:unbindservice (connection);//Unbind Service Break; default: Break; } }}
Android Services (1)