Waiting for asynchronous callback in Android's IntentService

后端 未结 6 1618
栀梦
栀梦 2021-02-05 03:08

I have an IntentService that starts an asynchronous task in another class and should then be waiting for the result.

The problem is that the IntentSer

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 03:58

    Use the standard Service class instead of IntentService, start your asynchronous task from the onStartCommand() callback, and destroy the Service when you receive the completion callback.

    The issue with that would be to correctly handle the destruction of the Service in the case of concurrently running tasks as a result of the Service being started again while it was already running. If you need to handle this case, then you might need to set up a running counter or a set of callbacks, and destroy the Service only when they are all completed.

提交回复
热议问题