“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
A Service is an application component that runs operations in the background for a long time without a user interface. A service can be started by another application component (such as an Activity), and once started, it runs in the background, even if the component that started the service is destroyed. In addition, components can be bound to services to interact with or even perform interprocess communication (IPC). For example, a Service can handle network transactions, play music, perform file I/O, or interact with a content provider, all in the background. Services come in basically two forms:
1. Startup status
When an application component, such as an Activity, starts a service by calling startService(), the service is in the “started” state. Once started, a service can run in the background indefinitely, even if the component that started it has been destroyed, and can only be stopped by a manual call, which typically performs a single operation and does not return the results to the caller.
2. Binding status
When an application component is bound to a service by calling bindService(), the service is in the “bound” state. Binding services provide a client-server interface that allows components to interact with services, send requests, get results, and even perform these operations across processes using interprocess communication (IPC). The binding service runs only when it is bound to another application component. Multiple components can be bound to the service at the same time, but when all are unbound, the service is destroyed.
OnBind (): This method is called when another component wants to bind to a service (for example, to execute RPC) by calling bindService(). In the implementation of this method, you must return an implementation class of the IBinder interface that the client can use to communicate with the service. This method must be overridden in either the start or bind state, but returns NULL directly in the start state. OnCreate (): This method is called when the service is first created to perform a one-time setup (before onStartCommand() or onBind() is called). This method is not called if the service is already running, but onStartCommand() is called only once: when another component (such as an Activity) requests to start the service by calling startService(). Once this method is executed, the service is started and can run in the background indefinitely. If you implement this method yourself, you need to stop the service by calling stopSelf() or stopService() after the service work is done. (There is no need to implement this method in the binding state.) OnDestory (): This method is called when the service is no longer in use and will be destroyed. The service should implement this method to clean up all resources, such as threads, registered listeners, sinks, and so on, which is the last call received by the service.Copy the code