android - context.startForegroundService(startServiceIntent) 多次会调用 onCreate 多次?
问题描述
如果我多次调用 context.startForegroundService(startServiceIntent),我会多次调用 onCreate() 并使用新的服务意图,还是我会第一次调用 onCreate 以获得新的服务意图,然后多次调用 onStartCommand()/onHandleIntent()次?
解决方案
这取决于服务是否正在运行。如果该服务尚未运行,startForegroundService()
将触发调用。onCreate()
因此,例如:
- 你打电话
startForegroundService()
- Android 创建一个服务实例并调用
onCreate()
它 - Android 然后调用
onStartCommand()
它(这可能会触发对其他事物的调用,例如onHandleIntent()
ofIntentService
) - 你
startForegroundService()
再打电话 - Android意识到你有服务的运行副本并没有创建一个新的,所以没有
onCreate()
调用 - Android 然后调用
onStartCommand()
它(这可能会触发对其他事物的调用,例如onHandleIntent()
ofIntentService
) - 您通过类似
stopService()
或stopSelf()
(或onHandleIntent()
返回,如果您仍在使用IntentService
)停止您的服务 - 你又打电话
startForegroundService()
了,因为你真的很喜欢那种方法 - Android 创建一个服务实例并调用
onCreate()
它 - Android 然后调用
onStartCommand()
它(这可能会触发对其他事物的调用,例如onHandleIntent()
ofIntentService
)
推荐阅读
- python - 如何在熊猫中嵌套 value_counts
- javascript - 如何将 GraphQL 服务器添加到预先存在的 SQL 数据库?
- algorithm - 将 n 个值拆分为 m 个组时避免明显代价高昂组合的算法
- android - 调试适配器中未处理的错误:SyntaxError: 位置 0 处 JSON 中的意外标记 L
- vba - 需要帮助,VBA,停止来自另一个私人子的私人子事件
- cordova - 离子 InAppBrowser 显示 err_unknown_url_scheme
- javascript - 如何在 react native 或 JS 中解码特殊字符或 HTML 实体?
- python - Tensorflow While loop with Variable Creation
- android - Can HorizontalScrollView be disable on touchevent and accessible using button
- firebase - 如果应用程序关闭/滑动(React Native)收到通知时崩溃