【发布时间】:2010-08-30 04:07:52
【问题描述】:
所以我了解服务生命周期以及所有这些。但我很困惑startId 参数的用途是什么?
public int onStartCommand (Intent intent, int flags, int startId)
我知道它与stopSelf(int) 结合使用,但我看不出重点是什么或生成 startId 的位置。使用 stopSelf(int) 属于什么用例?
【问题讨论】:
所以我了解服务生命周期以及所有这些。但我很困惑startId 参数的用途是什么?
public int onStartCommand (Intent intent, int flags, int startId)
我知道它与stopSelf(int) 结合使用,但我看不出重点是什么或生成 startId 的位置。使用 stopSelf(int) 属于什么用例?
【问题讨论】:
使用 stopSelf(int) 属于什么用例?
第 1 步:致电startService()
步骤#2:再次致电startService()
步骤#3:致电stopSelf()
此时,我们希望服务不要停止,因为仍有未完成的工作,由第二个 startService() 调用表示。我们希望 startService() 和 stopSelf() 调用匹配。
【讨论】:
IntentService 并考虑这是否是您使用的更好的基类。它自己处理 stopSelf(int) 调用以及其他功能。
startId 作为参数传递给您的代码,直到您到达stopSelf() 需要它的地步。