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
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:
2.2 stopService()
- Role: close the Service service
- After manually calling stopService(), the internal method is automatically called: onDestory()
- Called logic:
2.3 bindService ()
- Role: Bind the Service service
- After manually calling bindService(), the internal methods are automatically called: onCreate(), onBind()
- Called logic:
2.4 unbindService()
- Role: Unbind the Service service
- After manually calling unbindService(), the internal methods are automatically called: onCreate(), onBind(), onDestory()
- Called logic:
3. Common Lifecycle Uses
3.1 Only use startService to start the life cycle of the service
3.2 Use only BindService to bind the service life cycle
3.3 同时使用startService()启动服务、BindService()绑定服务的生命周期
3.4 特别注意
- startService()和stopService()只能开启和关闭Service,无法操作Service;
bindService()和unbindService()可以操作Service
- startService开启的Service,调用者退出后Service仍然存在;
BindService开启的Service,调用者退出后,Service随着调用者销毁。
4. 总结
- 本文对Service的生命周期进行了全面解析
-
如果你还想了解关于Service的其他知识,请浏览以下文章:
Android四大组件:Service史上最全面解析
Android:Service生命周期最全面解析
Android:(本地、可通信的、前台、远程)Service使用全面介绍
Android:远程服务Service(含AIDL & IPC讲解)
Android多线程全面解析:IntentService用法&源码 -
接下来会介绍继续介绍Android开发中的相关知识,有兴趣可以继续关注Carson_Ho的安卓开发笔记
请点赞!因为你们的赞同/鼓励是我写作的最大动力!
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)