[Service of the four major components of Android] Understand the Service service in one article

Author: Half Body Blizzard
Introduction: The most basic and most common four components in Android components:

  • Activity
  • Service
  • Content Provider
  • BroadcastReceiverBroadcast receiver

Understand Service in one article

1. What is Service

Service is one of the four components in the Android system. It is a service program with a long [life cycle] and no visual interface that runs in the background .

There are two ways to start a Service:

  1. startService start
  2. bindService starts

2. How to start the Service

2.1, startService display startup

2.1.1, Service start

  1. First we create a MyService class and inherit from Service

public class MyService extends Service {
    public IBinder onBind(Intent intent) {
        return null;

The onBind method needs to be implemented first , which onBind()is a method that Service must implement. The returned IBinder object is equivalent to the proxy object of the Service component. Service allows other program components to access the internal data of the Service through the IBinder object, so that the relationship between other program components and the Service can be realized. communication between.

  1. Register in AndroidManifest.xmlmanifest file

<!-- The component must be registered in the manifest file --> 
< service  android:name = ".MyService" />

  1. Explicit startup is started by the class name, you need to specify the class where the Service is located in the Intent, and call startService (lntent) to start the service. Here I use the button test, I need to add a serviceStart button to the activity to demonstrate

public void onClick(View view) {
    startService(new Intent(this, MyService.class));

  1. Go back to the MyService class and add an onCreate lifecycle to verify our startup

public void onCreate() {

    System .out .println (" MyService .onCreate  service started successfully");

  1. operation result

By starting the Service in this way, there is no association between the visitor and the Service. Even if the visitor exits, the Service still runs.

According to the above statement, even if the visitor exits, the Service is still running , so how do we end it?

Service stopped

  1. We in the Activity, add a stop button.

    To explicitly start and stop a Service, you need to pass the Intent that starts the Service to the stopService (Intent) function.

public void onClickStop(View view) {
    stopService(new Intent(this, MyService.class));


  1. Go back to the MyService class and add an onDestroy lifecycle to verify our stop

public void onDestroy() {

    System .out .println ( " MyService .onDestroy service stopped"); 

  1. operation result

I said above that startService is an explicit start, so is there a corresponding implicit start? There is indeed, but starting from Android5.0, Google requires that the Service must be started with the display Intent, so we will not introduce the implicit start.

2.2, bindService binding start

2.2.1. Use the bindService() method to start the Service

  1. We are back in the Activity and add two more buttons, the onClickBindServiceStart and onClickUnBindService buttons

public void onClickBindServiceStart(View view) {

    bindService(new Intent(this, MyService.class), connection, Context.BIND_AUTO_CREATE);


Parameter Description:

  • service: This parameter specifies the service to be started through Intent.
  • connection: This parameter is the ServiceConnnection object. When the binding is successful, the system will call the serviceConnnection onServiceConnected ()method. When the binding is disconnected unexpectedly, the system will call the onServiceDisconnected method in the ServiceConnnection.
  • flags: This parameter specifies whether to automatically create a Service when binding. If specified as BIND_AUTO_CREATE, it will be created automatically, if specified as 0, it will not be created automatically.

  • Initialize a connection

private ServiceConnection connection = new ServiceConnection() {
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {


    public void onServiceDisconnected(ComponentName componentName) {


In the binding startup, when the caller bindService()binds the Service through the function, the onCreate()function and onBinde ( )the function will be called successively.

  1. operation result

By starting the Service in this way, the visitor is bound to the Service, and once the visitor exits, the Service is terminated.

2.2.2, unbindService stop service

Unbinding only requires using the unbindService()method and passing the ServiceConnection to the unbindService()method

  1. Use unbindService()method

public void onClickUnBindService(View view) {

  1. Go back to the MyService class and add the onUnbind lifecycle method

public boolean onUnbind(Intent intent) {

    return super.onUnbind(intent);

The function will be called when the caller unbindService()unbinds the Service through the function. onUnbind()If the onUnbind()function returns true , it means that the onRebind ()function will be called when the service is re-bound.

  1. operation result

It should be noted that unbindService()after the method is successful, the system will not call onServiceConnected()it, because it onServiceConnected()will only be called when the binding is accidentally disconnected.

Don’t forget to onDestroy()destroy bindService here

protected void onDestroy() {

3. Service life cycle

3.1, the life cycle of startService startup

onCreate() is called by the system when the Service is first created.
onStartCommand() This method is called when the startService method starts the Service.
onDestroy() is called by the system when the Service is no longer in use.

Note: A startService can only be created once and destroyed once, but can be started multiple times, so the onCreate() and onDestroy() methods will only be called once, and the onStart() method will be called multiple times.

3.2, the life cycle of bindService startup

onCreate() is called by the system when the Service is created.
onBind() This method is called when the bindService method starts the Service.
onUnbind() This method is called when the unbindService method is unbound.
onDestroy() is called by the system when the Service is no longer in use.

Note: A bindService can be created multiple times, destroyed multiple times, and reused.

Leave a Comment

Your email address will not be published. Required fields are marked *