service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径:

A started service

  被开启的service通过其他组件调用 startService()被创建。
  这种service可以无限地运行下去,必须调用stopSelf()方法或者其他组件调用stopService()方法来停止它。
  当service被停止时,系统会销毁它。
 

A bound service

  被绑定的service是当其他组件(一个客户)调用bindService()来创建的。
  客户可以通过一个IBinder接口和service进行通信。
  客户可以通过 unbindService()方法来关闭这种连接。
  一个service可以同时和多个客户绑定,当多个客户都解除绑定之后,系统会销毁service。
 
  这两条路径并不是完全分开的。
  即是说,你可以和一个已经调用了 startService()而被开启的service进行绑定。
  比如,一个后台音乐service可能因调用 startService()方法而被开启了,稍后,可能用户想要控制播放器或者得到一些当前歌曲的信息,可以通过bindService()将一个activity和service绑定。这种情况下,stopService()或 stopSelf()实际上并不能停止这个service,除非所有的客户都解除绑定。
 

Implementing the lifecycle callbacks

  和activity一样,service也有一系列的生命周期回调函数,你可以实现它们来监测service状态的变化,并且在适当的时候执行适当的工作。
  下面的service展示了每一个生命周期的方法:
public class ExampleService extends Service
{
int mStartMode; // indicates how to behave if the service is killed
IBinder mBinder; // interface for clients that bind
boolean mAllowRebind; // indicates whether onRebind should be used @Override
public void onCreate()
{
// The service is being created
} @Override
public int onStartCommand(Intent intent, int flags, int startId)
{
// The service is starting, due to a call to startService()
return mStartMode;
} @Override
public IBinder onBind(Intent intent)
{
// A client is binding to the service with bindService()
return mBinder;
} @Override
public boolean onUnbind(Intent intent)
{
// All clients have unbound with unbindService()
return mAllowRebind;
} @Override
public void onRebind(Intent intent)
{
// A client is binding to the service with bindService(),
// after onUnbind() has already been called
} @Override
public void onDestroy()
{
// The service is no longer used and is being destroyed
}
}

不像是activity的生命周期回调函数,你不需要调用基类的实现。

这个图说明了service典型的回调方法,尽管这个图中将开启的service和绑定的service分开,但是你需要记住,任何service都潜在地允许绑定。
  所以,一个被开启的service仍然可能被绑定。
  实现这些方法,你可以看到两层嵌套的service的生命周期:
 

The entire lifetime

  service整体的生命时间是从onCreate()被调用开始,到onDestroy()方法返回为止。
  和activity一样,service在onCreate()中进行它的初始化工作,在onDestroy()中释放残留的资源。
  比如,一个音乐播放service可以在onCreate()中创建播放音乐的线程,在onDestory()中停止这个线程。
   onCreate() 和 onDestroy()会被所有的service调用,不论service是通过startService()还是bindService()建立。
 

The active lifetime

  service积极活动的生命时间(active lifetime)是从onStartCommand() 或onBind()被调用开始,它们各自处理由startService()或 bindService()方法传过来的Intent对象。
  如果service是被开启的,那么它的活动生命周期和整个生命周期一同结束。
  如果service是被绑定的,它们它的活动生命周期是在onUnbind()方法返回后结束。
  注意:尽管一个被开启的service是通过调用 stopSelf() 或 stopService()来停止的,没有一个对应的回调函数与之对应,即没有onStop()回调方法。所以,当调用了停止的方法,除非这个service和客户组件绑定,否则系统将会直接销毁它,onDestory()方法会被调用,并且是这个时候唯一会被调用的回调方法。
 

Managing the Lifecycle of a Bound Service

  当绑定service和所有客户端解除绑定之后,Android系统将会销毁它,(除非它同时被onStartCommand()方法开启)。
  因此,如果你的service是一个纯粹的绑定service,那么你不需要管理它的生命周期。
  然而,如果你选择实现onStartCommand()回调方法,那么你必须显式地停止service,因为service此时被看做是开启的。
  这种情况下,service会一直运行到它自己调用 stopSelf()或另一个组件调用stopService(),不论它是否和客户端绑定。
  另外,如果你的service被开启并且接受绑定,那么当系统调用你的 onUnbind()方法时,如果你想要在下次客户端绑定的时候接受一个onRebind()的调用(而不是调用 onBind()),你可以选择在 onUnbind()中返回true。
  onRebind()的返回值为void,但是客户端仍然在它的 onServiceConnected()回调方法中得到 IBinder 对象。
  下图展示了这种service(被开启,还允许绑定)的生命周期:

Activity Fragment Service生命周期图的更多相关文章

  1. android activity和fragment的生命周期图

    Activity的生命周期: Fragment的生命周期:

  2. Activity与Service生命周期

    一. Activity 先展示一张Activity的生命周期图: 1.1 Activity状态 只有下面三个状态是静态的,可以存在较长的时间内保持状态不变.(其它状态只是过渡状态,系统快速执行并切换到 ...

  3. Activity状态图、生命周期图(超详细),onSaveInstanceState只保存、恢复基本ui数据,持久数据不在这里保存。

    1.Activity状态图 2.Activity生命周期简图 启动Activity: onCreate()—>onStart()—>onResume(),Activity进入running ...

  4. Android之Activity与fragment完整生命周期图

    转自:https://github.com/xxv/android-lifecycle

  5. Android 特别大的Activity和Fragment的生命周期图

    这么 这么大的图.不做太多解释,哈哈,真的是棒棒的. 代码測试下载:http://download.csdn.net/detail/pcaxb/8906085

  6. fragment的生命周期及其各个周期方法的作用

    先上生命周期图: Fragment的生命周期图: 与Activity的生命周期对比图: 由于Fragment是嵌在Activity中使用的,故其生命周期也是依赖于Activity的周期的,或者说Fra ...

  7. 死磕 Fragment 的生命周期

    死磕 Fragment 的生命周期 本文原创,转载请注明出处.欢迎关注我的 简书 ,关注我的专题 Android Class 我会长期坚持为大家收录简书上高质量的 Android 相关博文.本篇文章已 ...

  8. Activity与Fragment的生命周期

    今天看到一张图,详细描述了Activity和Fragment的生命周期,好资源共享咯!

  9. Activity生命周期图

    首先看一下Android api中所提供的Activity生命周期图 Activity其实是继承了ApplicationContext这个类,我们可以重写以下方法,如下代码: public class ...

随机推荐

  1. canvas 实现渐变色填充的三角形

    实现效果 效果一: 效果二: 实现思路 canvas实现 1. 绘制三角形 // html <canvas id="triangle" width="30" ...

  2. Linux(centos)创建用户并分配权限

    创建名为 elas的用户 adduser elas 初始化elas的密码 passwd elas 显示 新的 密码: 重新输入新的 密码: passwd:所有的身份验证令牌已经成功更新. 进行授权 个 ...

  3. mac osx 准备 nanogui 记录

    !!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist mac osx : 10.14.6 Mojave. 之前没有配置openGL相关开发环境,自 ...

  4. Andrey and Problem

    B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. golang 数组的一些自问自答

    所有代码基于Go-1.17.一些研究Go数组的自问自答,可以考虑作为面试题. 问题:静态存储区是什么?和堆/栈有什么区别? 回答: 可以参考下列图 堆上存放new产生的大块内存 栈上存放的是程序运行的 ...

  6. visual studio code的python编程中遇到的SyntaxError:invalid syntax问题的原因和解决办法

    一.问题重现描述 1.最开始,正常执行语句 2.当我在"终端窗口"输入python进入交互模式后,再选择默认的"在终端中运行python文件"运行代码报错 3. ...

  7. IM2603设计资料 Type-C拓展坞电源管理芯片

    应用于Type-C拓展坞外围集成Buck变换器的电源管理芯片 IM2603 IM2603 概述 用于带有集成降压转换器的 Type-C 外围应用的电源管理 IC IM2603 是一款主要用于 Type ...

  8. Java程序设计基础笔记 • 【第10章 数组】

    全部章节   >>>> 本章目录 10.1 数组概述 10.1.1 数组优势 10.1.2 Java中的数组 10.1.3 数组的分类 10.2 一维数组 10.2.1 数组的 ...

  9. Vulnhub实战-rtemis靶机👻

    Vulnhub实战-rtemis靶机 下载地址:http://www.vulnhub.com/entry/r-temis-1,649/ 描述 通过描述我们知道这个靶机有两个flag 主机发现 通过nm ...

  10. RSA非对称加密算法实现:C#

    RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...