Multiple IntentService or one Service

后端 未结 3 397
醉话见心
醉话见心 2021-01-31 10:36

I\'m a little confused on the difference between IntentService and Service. I understand that IntentService has a worker queue, but is there any benefit to using multiple Intent

相关标签:
3条回答
  • 2021-01-31 11:21

    IntentService is just a convenient class to write services that are workers in the producer-consumer pattern. They are services designed to execute various tasks in a row and then stop. Services are not necessarily IntentServices such as services that must stay alive such as daemons.

    So you should wonder if you service is close to a worker thread, if so, use IntentServices else just derive from Service.

    Your second questions was whether to group all 3 services in a 3 in 1 service. The answer is that it depends how you use your datasources : if you use them altogether, then group them in a single service. If they are used separately, you could build a service for each with the hope to provide a lighter service if only one datasource is used and not the other. But if you use all 3 datasources, each in a service, then it will be heavier than using a single service.

    0 讨论(0)
  • 2021-01-31 11:35

    Its my understanding that the difference between intentService and Service is that an intentService will spawn a worker thread to run it, while a Service runs in the main thread of it's hosting process. In addition, an intentService will stop itself when the work is done, while a Service will continue running until stopSelf, or stopService is called.

    If the 3 data sources need to share information with each other, then put them all in the same Service, otherwise keep them separate because if one data source is down it will leave a fat service running instead of just a single light Service.

    0 讨论(0)
  • 2021-01-31 11:39

    To allow multiple tasks to run at the same time off the main thread, you need to provide a managed collection of threads. Use a ThreadPoolExecutor to manage multiple threads at the same time:

    http://developer.android.com/training/multiple-threads/create-threadpool.html

    0 讨论(0)
提交回复
热议问题