【发布时间】:2018-03-15 15:28:18
【问题描述】:
谁能告诉我如何在 Android 中停止intentservice?
如果我使用stopservice 来停止一个意图服务,它就不起作用。
我浏览过网站,但没有人明确回答这个问题。
【问题讨论】:
标签: android
谁能告诉我如何在 Android 中停止intentservice?
如果我使用stopservice 来停止一个意图服务,它就不起作用。
我浏览过网站,但没有人明确回答这个问题。
【问题讨论】:
标签: android
尝试从服务中调用stopSelf()。 documentation 表示某些进程可能需要很长时间才能完成,因此您的服务很可能正在等待它们停止。你可能应该调查一下。完成所有请求后,服务会自行停止。
【讨论】:
stopService 没有帮助,那么一个进程正在占用它。 protected abstract void onHandleIntent (Intent intent) Since: API Level 3 This method is invoked on the worker thread with a request to process. Only one Intent is processed at a time, but the processing happens on a worker thread that runs independently from other application logic. So, if this code takes a long time, it will hold up other requests to the same IntentService, but it will not hold up anything else. When all requests have been handled, the IntentService stops itself, so you should not call stopSelf().
IntentService 被构建为在 onHandleIntent(Intent) 处理最后一个 Intent 时自行停止 - 即您无需停止它,它在完成后自行停止。
【讨论】:
根据documentation,Android 会在处理完所有启动请求后停止服务,因此您无需调用 stopSelf()。
【讨论】:
如果有人遇到同样的问题,请及时回复。如果您调用<yourContext>.stopService(intent),其中intent 是您用于启动服务的相同意图对象,那么它将起作用。
【讨论】:
IntentService 旨在仅在处理工作队列中存在的所有请求时自行停止。根据文档,IntentService 类 “在处理完所有启动请求后停止服务,因此您永远不会调用 stopSelf()"
【讨论】:
Intent 服务使用
自动停止stopSelf()
仅当工作队列中存在的所有请求都已处理完毕时。
【讨论】: