【问题标题】:Proper way to stop IntentService停止 IntentService 的正确方法
【发布时间】:2012-04-20 17:25:15
【问题描述】:

我正在使用 IntentService 将图像上传到服务器。我的问题是我不知道如何/何时停止服务。当我在 onHandleIntent(Intent ..) 中调用 stopself() 时,所有在 IntentService 队列中等待的 Intent 都会被删除。但我不想从活动中停止服务,因为即使我的应用程序没有运行,我也想完成上传过程。

【问题讨论】:

    标签: android intentservice


    【解决方案1】:

    我的问题是我不知道如何/何时停止服务。

    IntentServiceonHandleIntent() 结束时自动停止,如果在onHandleIntent() 运行时没有向它发送更多命令。因此,您不必自己手动停止IntentService

    当我在 onHandleIntent(Intent ..) 中调用 stopself() 时,所有在 IntentService 队列中等待的 Intent 都会被删除。

    这就是你不这样做的原因。

    但我不想从活动中停止服务,因为即使我的应用程序没有运行,我也想完成上传过程。

    然后让IntentService 自行停止。

    【讨论】:

    • 谢谢,我不知道 IntentService 完成后会自行停止。:]
    • Android SDK 中又一个锯齿状的角落...叹息。
    • 我这里有这个问题,应该是 25 秒到 1 分钟的任务没有结束,所以我的 intentService 没有完成 - stackoverflow.com/questions/39976001/…
    【解决方案2】:

    当没有更多的Intents(作业)要处理时,IntentService 会自行停止。它为每个Intent(作业)运行stopSelf(startId)。看看IntentService 源或Extending the Service Class 从这里http://developer.android.com/guide/topics/fundamentals/services.html

    【讨论】:

      猜你喜欢
      • 2010-09-26
      • 2023-03-28
      • 2013-11-24
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多