`
modabobo
  • 浏览: 509418 次
文章分类
社区版块
存档分类
最新评论

【Android笔记】Service的生命周期

 
阅读更多

AndriodService的生命周期

1. Service生命周期

1) 可以通过调用Context.startService()启动一个Service,这可能会触发ServiceonCreate()onStart()操作,具体来说即执行startService()一定会触发onStart(),但如果该Service已经在系统中存在,则onCreate()不会被再次调用,它只在Service第一次启动时触发。

通过Context.startService()启动的Service会一直运行,直到通过Context.stopService()或者stopSelf()停止它。多次通过startService()启动某个服务并不会生成多个实例,但会导致服务的onStart()被多次调用,当然由于只有一个实例,因此无论启动多少次,停止它只需调用一次Context.stopService()stopSelf()就可以了。

2) 也可以通过Context.bindService()来获得一个服务的链接,这个链接是一直会保持到通过Context.unbindService()断掉它。如果在连接时系统中还没有该服务,则可能会新创建一个服务,这时ServiceonCreate函数也同样会被调用。连接建立时会ServiceonBinder会被触发,通过onBinder可以返回连接建立后的IBinder接口对象,使用服务的客户端(比如某个Activity)可以通过IBinder对象和Service交互。

一个Service如果是通过bindService()启动的,那么它会一直存在到没有任何客户端与它保持连接为止,原因是可能有很多客户端与这个服务保持连接,这时如果某个链接被客户端主动断掉只会是Service的链接数减1,当减至0的时候这个Service就会被销毁。

3) 一个Service既可以被启动(start)也可以被连接(bind),这时Service的生命周期取决于它被创建的方式,如果是通过Context.startService()创建的则和第一种情况一样,如果是通过Context.bindService()使用参数Context.BIND_AUTO_CREATE创建的,则情况和第二种一样。

当然,在Service停止,被销毁时,会触发其onDestroy()函数,我们需要在这里完成这个Service相关资源的清理,比如停止其子线程,注销监听器等等。

2. 相关的官方描述(Android SDK1.5)如下:

android-sdk文档 /docs/reference/android/app/Service.html

1) There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate()method if needed) and then call its onStart(Intent, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStart()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called.

2) Clients can also use Context.bindService() to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate() while doing so), but does not call onStart(). The client will receive the IBinder object that the service returns from its onBind(Intent) method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the service's IBinder). Usually the IBinder returned is for a complex interface that has been written in aidl.

3) A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated. All cleanup (stopping threads, unregistering receivers) should be complete upon returning from onDestroy().

如下是Service的生命周期时序图:

3. 几个需要注意的地方

1) Service无论以何种方式创建,都是在应用的主线程里创建的,也就是说创建一个Service并不意味着生成了一个新的线程,Service的创建过程是阻塞式的,因此也需要考虑性能,不能影响界面和逻辑上的后续操作。

2) 如果Service自己没有生成新的线程,那它也是运行在应用的主线程里的,因此Service本身并不能提高应用的响应速度和其他的性能,而是说通过这个后台服务生成新的线程来处理比较耗时的操作如大数据的读取等来提高响应,Service自己并不能保证这一点。Service相当于提供了一个这些费时操作的平台,由它在后台创建新线程完成这些任务,以及视各种情况管理这些线程,包括销毁。

3) stopServiceunbindService都可以把Service停掉,但是如果希望明确立刻停掉Service,则使用stopService更安全,因为unbindService实质上是将与Service的连接数减一,当减为0的时候才会销毁该服务实例,stopService的效果相当于将连接数立即减为0,从而关闭该服务,所以在选择关闭方式上要视不同情况而定。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics