How to force an IntentService to stop immediately with a cancel button from an Activity?

后端 未结 9 895
梦如初夏
梦如初夏 2020-11-30 00:10

I have an IntentService that is started from an Activity and I would like to be able to stop the service immediately from the activity with a \"cancel\" button in the activi

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 01:09

    In case of IntentService it does not stop or takes any other request through some intent action until its onHandleIntent method completes the previous request.

    If we try to start IntentService again with some other action, onHandleIntent will be called only when previous intent / task is finished.

    Also stopService(intent); or stopSelf(); does not work until the onHandleIntent() method finishes its task.

    So I think here better solution is to use normal Service here.

    I hope it will help!

提交回复
热议问题