service的生命周期
Managing the Lifecycle of a Service
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(被开启,还允许绑定)的生命周期:

使用context.startService() 启动Service
其生命周期为context.startService() ->onCreate()- >onStart()->Service running-->context.stopService() | ->onDestroy() ->Service stop 如果Service还没有运行,则android先调用onCreate()然后调用onStart(); 如果Service已经运行,则只调用onStart(),所以一个Service的onStart方法可能会重复调用多次。
stopService的时候直接onDestroy, 如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。 该Service的调用者再启动起来后可以通过stopService关闭Service。
所以调用startService的生命周期为:onCreate --> onStart(可多次调用) --> onDestroy
对于bindService()启动Service会经历: context.bindService()->onCreate()->onBind()->Service running-->onUnbind() -> onDestroy() ->Service stop
onBind将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service运行的状态或其他操作。 这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了, Srevice就会调用onUnbind->onDestroy相应退出。
所以调用bindService的生命周期为:onCreate --> onBind(只一次,不可多次绑定) --> onUnbind --> onDestory。 一但销毁activity它就结束,如果按home把它放到后台,那他就不退出。
补充: 在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用), 其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。
service的生命周期的更多相关文章
- Android activity和service的生命周期对比
1Activity生命周期 七个方法 1. void onCreate(Bundle savedInstanceState) 当Activity被第首次加载时执行.我们新启动一个程序的时候其主窗体的o ...
- Android 测试Service的生命周期
package com.example.myapp4; import android.support.v7.app.ActionBarActivity; import android.content. ...
- Activity和Service的生命周期(图)
1.Activity的生命周期 情形一.一个单独的Activity的正常的生命过程是这样的:onCreate->onStart->onPause->onStop->onDest ...
- Android Service的生命周期
service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径: A started service 被开启的service通过其他组件调用 startService()被创建. 这种 ...
- 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景
============================================================================ 服务的生命周期: 一.采用start的方式开始 ...
- android基础---->service的生命周期
服务是一个应用程序组件代表应用程序执行一个长时间操作的行为,虽然不与用户交互或供应功能供其它应用程序使用.它和其他的应用对象一样,在他的宿主进程的主线程中运行.今天我们开始android中普通serv ...
- android学习-Activity和Service的生命周期
详细请跳转原网页Activity和Service的生命周期(图) 不解释,不懂算我输 Activity的生命周期(图) Service的声明周期
- 8.1.1 Service的生命周期
2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T <Android/OPhone开发完全讲义>第8章Android服务,本章主要介绍了Android系统 中的服 ...
- service的生命周期以及两种service的差异
可以看到,两种service的生命周期都相对简单,有一点不同的是,Intentservice每次调用的时候都执行onstartcommand,而boundservice一旦启动了之后,就不会每次执行o ...
随机推荐
- iOS app签名原理
基本原理: 公钥能够验证私钥的签名是否正确. Apple后台有一个私钥A,iOS内置一个公钥A,与私钥A对应.(A:代表Apple,即苹果) 本地产生一对公钥L.私钥L,(L:代表Local,即本地) ...
- GATT服务搜索流程(二)
关于bta_dm_cb.p_sec_cback,这里我们之前已经分析过,他就是bte_dm_evt ,最终调用的函数btif_dm_upstreams_evt : static void btif_d ...
- 换了电脑如何使用hexo继续写博客
前言 我们知道,使用 Github+hexo 搭建一个个人博客确实需要花不少时间的,我们搭好博客后使用的挺好,但是如果我们有一天电脑突然坏了,或者换了系统,那么我们怎么使用 hexo 再发布文章到个人 ...
- js格式化时间
转自:https://blog.csdn.net/u010964869/article/details/51095827 显示格式为:yyyy-mm-dd hh:mi:ss function form ...
- linux第三次实践:ELF文件格式分析
linux第三次实践:ELF文件格式分析 标签(空格分隔): 20135328陈都 一.概述 1.ELF全称Executable and Linkable Format,可执行连接格式,ELF格式的文 ...
- JVM EXCEPTION_ACCESS_VIOLATION
## A fatal error has been detected by the Java Runtime Environment:## EXCEPTION_ACCESS_VIOLATION (0x ...
- 兼容IE-FireFox-Chrome的背景音乐播放
以music目录下的kn.mp3文件为例: <bgsound src="music/kn.mp3" loop="-1"/> <audio sr ...
- SQLSERVER case when 的学习
sqlserver 查询时的CASE WHEN学习记录 ) as '任务数', RPATask_State as id, case RPATask_State when then '已接收' when ...
- Node require
var user = require("./module_user");//使用模块 module_userconsole.log(user.userCount);user.use ...
- centos7 搭建svn服务器
1.安装svn服务器: yum install subversion 2.配置svn服务器: 建立svn版本库根目录及相关目录即svndata及密码权限命令svnpasswd: mkdir -p /a ...