Service之startService,bindService交叉使用时的生命周期

本文详细解析了Android中服务的生命周期与绑定机制,包括startService、stopService、bindService和unbindService的调用顺序及回调方法。通过日志记录展示了服务创建、销毁以及与Activity之间的连接与断开的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.仅startService和stopService配合使用,与仅bindService,unbindService配合使用时,service生命周期是比较明确的

2.当startService,stopService,bindService,unbindService时,情况稍微复杂,但是通过打log可以清晰看到其中的调用过程,总结如下:

(1)针对startService和bindService:

a.先startService,则调用onCreate,onStartCommand;当bindService时,再调用onBind(不会再调用onCreate)

b.先bindServcie,则调用onCreate,onBind;当startService时,再调用onStartCommand

(2)针对stopService和unbindService:

a.先stopService,则不会调用任何方法,然后当unbindService时,依次调用onUnbind和onDestroy方法

b.先unbindService,则会调用onUnbind,然后当stopService时,会调用onDestroy


详细的log记录如下,其中,MyService中定义MyServiceBinder类(继承自IBinder),该类中定义了start方法(当Activity与service建立连接时,onServiceConnected中调用),end方法(当连接异常断开时,onServiceDisconnected中调用):

1.StartService

(1)start
12-02 07:39:50.986 24755-24755/com.example.wtx.service D/MyService: onCreate
12-02 07:39:50.986 24755-24755/com.example.wtx.service D/MyService: onStartCommand
(2)stop
12-02 07:40:31.731 24755-24755/com.example.wtx.service D/MyService: onDestroy

2.BindService
(1)bind
12-02 07:42:12.212 24755-24755/com.example.wtx.service D/MyService: onCreate
12-02 07:42:12.212 24755-24755/com.example.wtx.service D/MyService: onBind
12-02 07:42:12.226 24755-24755/com.example.wtx.service D/MyService: MyServiceBinder.start
(2)unbind
12-02 07:42:25.559 24755-24755/com.example.wtx.service D/MyService: onUnbind
12-02 07:42:25.559 24755-24755/com.example.wtx.service D/MyService: onDestroy

3.先Start再bind
(1)start
12-02 07:44:34.809 24755-24755/com.example.wtx.service D/MyService: onCreate
12-02 07:44:34.809 24755-24755/com.example.wtx.service D/MyService: onStartCommand
(2)bind
12-02 07:44:47.059 24755-24755/com.example.wtx.service D/MyService: onBind
12-02 07:44:47.075 24755-24755/com.example.wtx.service D/MyService: MyServiceBinder.start
(3)stop
无新log打印
(4)unbind
12-02 07:45:19.796 24755-24755/com.example.wtx.service D/MyService: onUnbind
12-02 07:45:19.796 24755-24755/com.example.wtx.service D/MyService: onDestroy
4.先Start再bind
(1)start
12-02 07:46:05.332 24755-24755/com.example.wtx.service D/MyService: onCreate
12-02 07:46:05.332 24755-24755/com.example.wtx.service D/MyService: onStartCommand
(2)bind
12-02 07:46:06.395 24755-24755/com.example.wtx.service D/MyService: onBind
12-02 07:46:06.409 24755-24755/com.example.wtx.service D/MyService: MyServiceBinder.start
(3)unbind
12-02 07:46:18.284 24755-24755/com.example.wtx.service D/MyService: onUnbind
(4)stop
12-02 07:46:56.315 24755-24755/com.example.wtx.service D/MyService: onDestroy

5.先Bind再start
(1)bind
12-02 07:48:04.821 24755-24755/com.example.wtx.service D/MyService: onCreate
12-02 07:48:04.821 24755-24755/com.example.wtx.service D/MyService: onBind
12-02 07:48:04.832 24755-24755/com.example.wtx.service D/MyService: MyServiceBinder.start
(2)start
12-02 07:48:18.636 24755-24755/com.example.wtx.service D/MyService: onStartCommand
(3)unbind
12-02 07:48:34.132 24755-24755/com.example.wtx.service D/MyService: onUnbind
(4)stop
12-02 07:48:45.197 24755-24755/com.example.wtx.service D/MyService: onDestroy
6.先Bind再start
(1)bind
12-02 07:51:01.932 11090-11090/com.example.wtx.service D/MyService: onCreate
12-02 07:51:01.932 11090-11090/com.example.wtx.service D/MyService: onBind
12-02 07:51:01.944 11090-11090/com.example.wtx.service D/MyService: MyServiceBinder.start
(2)start
12-02 07:51:09.613 11090-11090/com.example.wtx.service D/MyService: onStartCommand
(3)stop
无新log打印
(4)unbind
12-02 07:51:23.592 11090-11090/com.example.wtx.service D/MyService: onUnbind
12-02 07:51:23.592 11090-11090/com.example.wtx.service D/MyService: onDestroy

### Android StartService BindService生命周期详解 #### 一、StartService生命周期 当通过 `startService()` 方法启动一个 Service ,该 Service 将独立于客户端运行。即使启动它的组件被销毁,Service 仍然会继续运行直到显式调用 `stopSelf()` 或者其他组件调用了 `stopService()`。 以下是 StartService 的典型生命周期方法及其触发条件: - **`onCreate()`**: 当第一次创建 Service 会被调用一次[^2]。 - **`onStartCommand(Intent intent, int flags, int startId)`**: 每次调用 `startService()` 都会触发此方法。它接收传递过来的 Intent 对象以及一些标志位参数[^4]。 - **`onDestroy()`**: 当 Service 被停止或者终止调用。通常在此处释放资源或清理操作。 如果多次调用 `startService()` 不会重新创建新的实例而是再次回调 `onStartCommand()` 函数[^3]。 ```java @Override public void onCreate() { super.onCreate(); Log.d("Lifecycle", "onCreate called"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("Lifecycle", "onStartCommand called with startId: " + startId); return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.d("Lifecycle", "onDestroy called"); } ``` #### 二、BindService生命周期 当通过 `bindService()` 绑定到某个 Service,这个 Service 只会在有至少一个绑定存在的情况下保持活动状态。一旦所有的绑定都解除,则除非之前已经由 `startService()` 开始过,否则系统将会销毁该 Service 实例[^1]。 下面是 BindService 的主要生命周期阶段描述如下: - **`onCreate()`**: 如果当前还没有创建任何实例的话,在首次建立连接前先执行本函数。 - **`onBind(Intent intent)`**: 返回 IBinder 接口对象给调用方用于通信目的;每当有一个新客户成功绑定了服务端就会进入这里处理逻辑。 - **`onUnbind(Intent intent)`**: 在最后一个绑定断开之后立即调用,询问是否要重建绑定关系。 - **`onRebind(Intent intent)`**: 若前面选择了重连选项(`true`) ,那么当下次又有新绑定请求到来的候便会走这条路径而不是直接新建另一个 session。 - **`onDestroy()`**: 像上面提到的一样只有在没有任何剩余绑定且也没有通过 start 方式激活的前提下才会真正结束掉整个进程且回收内存空间。 下面是一个简单的例子展示如何实现可绑定的服务类定义: ```java private final IBinder binder = new LocalBinder(); @Override public IBinder onBind(Intent intent) { Log.d("Lifecycle", "onBind called"); return binder; } @Override public boolean onUnbind(Intent intent) { Log.d("Lifecycle", "onUnbind called"); return true; // Indicate whether you want to receive the onRebind(). } @Override public void onRebind(Intent intent) { Log.d("Lifecycle", "onRebind called"); } ``` #### 三、同使用 StartService BindService 的情况下的生命周期行为 在这种混合模式下,Service 不仅可以作为单独工作的后台任务来维持自己的生命期,还可以允许外部应用与其进行交互通讯。具体表现为以下几点特性: - 初始状态下可能因为某些原因既需要长期存活又得提供接口供第三方访问所以两者兼而有之; - 此刻无论哪一方主动发起关闭动作都不会立刻影响另一部分功能正常运作直至完全脱离关联为止才彻底消亡。 总结来说就是只要任意一种形式还有效就足以支撑起整体框架结构不崩溃倒塌下去而已。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值