Android: The most comprehensive analysis of the Service life cycle

foreword


  • As one of the four major components of Android, Service is widely used
  • This article will introduce the most basic knowledge of Service: the life cycle of Service 

If you don't know about Service yet, I suggest you read the article I wrote first: The 
Four Components of Android: The Most Comprehensive Analysis in the History of Service


content

content


1. Common methods of life cycle

In the service life cycle, commonly used are:

  • 4 manually invoked methods
call method manually effect
startService() start the service
stopService() shut down service
bindService () bind service
unbindService() Unbind service
  • 5 methods that are automatically called internally
The method that is automatically called internally effect
onCreat() Create service
onStartCommand() start service
onDestroy () Destruction service
unbind() bind service
onUnbind() Unbind service

2. A detailed introduction to the life cycle method

It mainly introduces the relationship between the internal calling method and the external calling method.

2.1 startService()

  • Role: start the Service service
  • After manually calling startService(), the internal methods are automatically called: onCreate(), onStartCommand()
  • The calling logic is as follows: 
    call logic

2.2 stopService()

  • Role: close the Service service
  • After manually calling stopService(), the internal method is automatically called: onDestory()
  • Called logic:

call logic

2.3 bindService ()

  • Role: Bind the Service service
  • After manually calling bindService(), the internal methods are automatically called: onCreate(), onBind()
  • Called logic:

call logic

2.4 unbindService()

  • Role: Unbind the Service service
  • After manually calling unbindService(), the internal methods are automatically called: onCreate(), onBind(), onDestory()
  • Called logic: 
    call logic

3. Common Lifecycle Uses

3.1 Only use startService to start the life cycle of the service

startService starts the life cycle of the service

3.2 Use only BindService to bind the service life cycle

The life cycle of BindService binding service

3.3 同时使用startService()启动服务、BindService()绑定服务的生命周期

Paste_Image.png

3.4 特别注意

  • startService()和stopService()只能开启和关闭Service,无法操作Service; 

    bindService()和unbindService()可以操作Service

  • startService开启的Service,调用者退出后Service仍然存在; 
    BindService开启的Service,调用者退出后,Service随着调用者销毁。

4. 总结


请点赞!因为你们的赞同/鼓励是我写作的最大动力!

Related articles Read  
Android Development: The Most Comprehensive and Understandable Android Screen Adaptation Solution  
Android Development: Comprehensive Analysis of Handler Asynchronous Communication Mechanism (including Looper, Message Queue)  
Android Development: The Most Comprehensive and Understandable Webview Detailed  
Android Development: Introduction to JSON and the most comprehensive analysis method!  
Android development: top Tab navigation bar implementation (TabLayout+ViewPager+Fragment)  
Android development: bottom Tab menu bar implementation (FragmentTabHost+ViewPager)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326099194&siteId=291194637