As with activity, service services are one of the four components of Android. Unlike activity, the service is not displayed, that is, there is no user interface, it is running in the background, but requires activity or other context to trigger.
A simple example is a music player in an Android phone that opens the app to see the interface is activity, and clicking on the music-enabled music service is served (can be heard but not seen.) For example, behind the scenes in the stage service, for everyone to operate the sound and lighting, but never appearance). And even if you quit the music player and still hear music on your desktop or other apps, when you stop, here's how the service life cycle begins.
Attach service life cycle diagram first
The two different implementations of the start service are the left and right of the figure.
The difference between the two ways to start:
by StartService () (left)
Service life cycle:
Context.startservice ()->oncreate ()->onstartcommand ()->service Runing-->context.stopservice () OnDestroy ()->service stop
StartService () After Oncreate->onstartcommand (note this is not onstart () because the OnStart () method has been eliminated and replaced by the Onstartcommand () method;
StopService () call OnDestroy () directly.
When the caller exits without StopService (), the service will continue to run in the background until the next caller StopService () or be strongly killed by the system.
by Bindservice () (right)
Service life cycle:
Context.bindservice ()->oncreate ()->onbind ()->service runing-->context.unbindservice ()->onUnbind ( )->ondestroy ()->service stop
Bindservice () after Oncreate->onbind ();
Unbindservice () is called Onunbind () and OnDestroy ().
When the caller binds the service through the Bindservice (), the caller and service become sworn brothers (not at the same time, but at the same time), and the caller exits and the service exits.
Attention:
1, the service OnCreate () will only be called once, that is, no matter how many times StartService or Bindservice, the service will only be created once.
For example, the first time StartService calls OnCreate () and Onstartcommand (), and the second StartService only calls Onstartcommand (), OnCreate () is not called again unless StopService is startservice again.
2. Onstartcommand () can be called multiple times (via multiple StartService calls) during each opening of the service, but OnCreate (), Onbind (), Onunbind (), Ondestory () Can only be called once
3, Onbindservice after the service can not be stopped through StopService, can only be stopped by Unbindservice after unbinding.
Here's an example to deepen your understanding
This is a user interface that starts and closes the service by clicking the button
View its life cycle through log logcat
Experiment One,
1. Click StartService
2. Click StartService again
Experiment Two,
1, click StartService after click StopService
2. Click StartService again
Experiment Three,
1. Click Bindservice
2. Then click Unbindservice or press the back button to exit
Experiment Four,
1. Click Bindservice
2. Click Bindservice again or click StopService
Experiment Five,
1, click Bindservice after click StartService
2. Then click Unbindservice
3. Then click StopService
Service life cycle of Android four components