【问题标题】:How to stop intentservice in android?如何在android中停止intentservice?
【发布时间】:2018-03-15 15:28:18
【问题描述】:

谁能告诉我如何在 Android 中停止intentservice

如果我使用stopservice 来停止一个意图服务,它就不起作用。

我浏览过网站,但没有人明确回答这个问题。

【问题讨论】:

    标签: android


    【解决方案1】:

    尝试从服务中调用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().
    • 你的意思是如果我调用stopService,一个intent会被发送到intentservice。如果intentservice已经有多个intent在队列中,“stopservice intent”在这些intent被处理之前不会被处理?
    • 事实上,我只是发现如果你在你的intentService中调用stopSelf(),当服务队列中还有意图等待时,等待的意图不会被执行。所以,我认为你不应该调用 stopSelf() ,除非你希望你的服务在完成工作后立即停止。
    【解决方案2】:

    IntentService 被构建为在 onHandleIntent(Intent) 处理最后一个 Intent 时自行停止 - 即您无需停止它,它在完成后自行停止。

    【讨论】:

      【解决方案3】:

      根据documentation,Android 会在处理完所有启动请求后停止服务,因此您无需调用 stopSelf()。

      【讨论】:

        【解决方案4】:

        如果有人遇到同样的问题,请及时回复。如果您调用<yourContext>.stopService(intent),其中intent 是您用于启动服务的相同意图对象,那么它将起作用。

        【讨论】:

          【解决方案5】:

          IntentService 旨在仅在处理工作队列中存在的所有请求时自行停止。根据文档,IntentService 类 “在处理完所有启动请求后停止服务,因此您永远不会调用 stopSelf()"

          【讨论】:

            【解决方案6】:

            Intent 服务使用

            自动停止

            stopSelf()

            仅当工作队列中存在的所有请求都已处理完毕时。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多