Home  >  Article  >  What is the service life cycle?

What is the service life cycle?

清浅
清浅Original
2019-05-09 14:41:2532442browse

The Service life cycle is divided into four parts, namely the life cycle of the started service, the life cycle of the bound service, the life cycle of the started and bound service, and the clearing of the service when the service is stopped.

What is the service life cycle?

Service life cycle

Similar to Activity, Service also has its own life cycle function. At different moments, The system will call the corresponding Service life cycle function, but compared with the Activity declaration cycle, the Service declaration cycle is simpler.

(1) Life cycle of the started service

If a Service is started by an Activity calling the Context.startService method, then regardless of whether there is an Activity bound using bindService Define or unbindService to unbind to the Service, and the Service will run in the background. If a Service is started multiple times by the startService method, the onCreate method will only be called once, onStart will be called multiple times (corresponding to the number of calls to startService), and the system will only create one instance of the Service. The Service will always run in the background, regardless of whether the corresponding program's Activity is running, until stopService or its own stopSelf method is called. Of course, if the system resources are insufficient, the Android system may also end the service.

(2) Life cycle of the bound service

If a Service is bound and started by an Activity calling the Context.bindService method, no matter how many times bindService is called, Every time, the onCreate method will only be called once, and the onStart method will never be called. When the connection is established, the Service will continue to run unless Context.unbindService is called to disconnect or the Context that previously called bindService no longer exists (such as when the Activity is finished), the system will automatically stop the Service and the corresponding onDestroy will be called.

(3) Life cycle of a service that is started and bound

If a Service is started and bound again, the Service will always be Background process. And no matter how it is called, onCreate will always be called only once, corresponding to how many times startService is called, Service's onStart will be called as many times. Calling unbindService will not stop the Service, but must call stopService or Service's stopSelf to stop the service.

(4) Clear the service when the service is stopped

When a Service is terminated (1. Call stopService; 2. Call stopSelf; 3. There is no longer a binding When a certain connection (has not been started)), the onDestroy method will be called. Here you should do some cleanup work, such as stopping the thread created and running in the Service.

The above is the detailed content of What is the service life cycle?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does jpeg mean?Next article:What does jpeg mean?