What happens if a Android Service is started multiple times?

后端 未结 4 883
一生所求
一生所求 2020-11-29 23:22

If I have the following code:

Intent intent = new Intent(this,DownloadService.class);     
for(int i=0;i

        
4条回答
  •  一向
    一向 (楼主)
    2020-11-30 00:17

    Adding some more information to the above answers which may be helpful for others is that, startId that the onStartCommand() method receives is different for every startService() call.

    Also if we write in for loop as mentioned above, code written in onHandleIntent() would execute so many times as defined by the for loop frequency, but in sequence and not in parallel.

    The concept is IntentService creates a work queue and each request to startService() trigger onStartCommand() which in turn stores the intent in work queue and then pass the intent one by one to onHandleIntent().

提交回复
热议问题