SoFunction
Updated on 2025-04-10

The difference between the two ways of starting Service (startservice and bindservice)

The Android Service life cycle can promote innovation in mobile devices and allow users to experience the best mobile services. Only broadcast receivers are activated when they execute this method. When onReceive() returns, it is an inactive state.

If no program stops it or it stops itself, the service will run all the time. In this mode, the service starts with calling() and stops with(). The service can stop itself by calling Android Service Lifecycle() or (). No matter how many times startService() is called, you only need to call stopService() once to stop the service.

It can be called by an external program through the interface. An external program establishes a connection to the service and operates the service through the connection. Establishing a connection starts with () and ends with (). Multiple clients can be bound to the same service. If the service is not started, bindService() can choose to start it.

The service cannot run itself, and the service needs to be started by calling the() or () method. Both methods can start the Service, but they are used differently. Use the startService() method to enable the service, and there is no connection between the caller and the service. Even if the caller exits, the service will still run. Use bindService() method to enable the service, and the caller is bound to the service. Once the caller exits, the service will terminate, which is very characteristic of "not seeking to live at the same time, but must die at the same time".

If you plan to use the () method to start the service, when the service is not created, the system will first call the service's onCreate() method, and then call the onStart() method. If the service has been created before calling the startService() method, multiple calls to the startService() method will not lead to multiple creation of the service, but will lead to multiple calls to the onStart() method. A service started with the startService() method can only call the () method to end the service, and the onDestroy() method will be called when the service ends.

If you plan to use the () method to start the service, when the service is not created, the system will first call the service's onCreate() method, and then call the onBind() method. At this time, the caller and the service are bound together. If the caller exits, the system will first call the service's onUnbind() method, and then call the onDestroy() method. If the service has been bound before calling the bindService() method, multiple calls to the bindService() method will not cause multiple service creation and binding (that is, onCreate() and onBind() methods will not be called multiple times). If the caller wants to unbind from the service being bound, he can call the unbindService() method. Calling the method will also cause the system call service's onUnbind()-->onDestroy() method.

Summary: Two startup methods and differences of Service

Service lifecycle methods onCreate, onStart, onDestroy

There are two ways to start a Service, and their impact on the Service Lifecycle is different.

1 via startService

Service will experience onCreate -> onStart

When stopService is onDestroy

If the caller exits directly without calling stopService, the Service will be running in the background. Next time the caller gets up again, you can stopService.

2 via bindService

Service will only run onCreate, and the caller of the service is bound to the service.

When the caller exits, Srevice will call onUnbind->onDestroyed. The so-called binding together will lead to survival and death. And this method can also make
The caller (for example) calls other methods on the service.