Service vs IntentService in the Android platform

后端 未结 11 603
挽巷
挽巷 2020-11-22 03:38

I am seeking an example of something that can be done with an IntentService that cannot be done with a Service (and vice-versa)?

I also bel

相关标签:
11条回答
  • 2020-11-22 03:53

    IntentService

    IntentService runs on its own thread. It will stop itself when it's done. More like fire and forget. Subsequent calls will be queued. Good for queuing calls. You can also spin multiple threads within IntentServiceif you need to- You can achieve this using ThreadPoolExecutor. I say this because many people asked me "why use IntentService since it doesn't support parallel execution". IntentService is just a thread. You can do whatever you need inside it- Even spinning multiple threads. The only caveat is that IntentService finishes as soon as you spin those multiple threads. It doesn't wait for those threads to come back. You need to take care of this. So I recommend using ThreadPoolExecutor in those scenarios.

    • Good for Syncing, uploading etc …

    Service

    By Default Service runs on the main thread. You need to spin a worker thread to do your job. You need to stop service explicitly. I used it for a situation when you need to run stuff in the background even when you move away from your app and come back more for a Headless service.

    • Again you can run multiple threads if you need to.
    • Can be used for apps like music players.

    You can always communicate back to your activity using BroadcastReceivers if you need to.

    0 讨论(0)
  • 2020-11-22 03:55

    I'm sure you can find an extensive list of differences by simply googling something such as 'Android IntentService vs Service'

    One of the more important differences per example is that IntentService ends itself once it's done.

    Some examples (quickly made up) could be;

    IntentService: If you want to download a bunch of images at the start of opening your app. It's a one-time process and can clean itself up once everything is downloaded.

    Service: A Service which will constantly be used to communicate between your app and back-end with web API calls. Even if it is finished with its current task, you still want it to be around a few minutes later, for more communication.

    0 讨论(0)
  • 2020-11-22 03:56

    An IntentService is an extension of a Service that is made to ease the execution of a task that needs to be executed in background and in a seperated thread.

    IntentService starts, create a thread and runs its task in the thread. once done, it cleans everything. Only one instance of a IntentService can run at the same time, several calls are enqueued.

    It is very simple to use and very convenient for a lot of uses, for instance downloading stuff. But it has limitations that can make you want to use instead the more basic (not simple) Service.

    For example, a service connected to a xmpp server and bound by activities cannot be simply done using an IntentService. You'll end up ignoring or overriding IntentService stuffs.

    0 讨论(0)
  • 2020-11-22 03:57

    Service

    • Invoke by startService()
    • Triggered from any Thread
    • Runs on Main Thread
    • May block main (UI) thread. Always use thread within service for long task
    • Once task has done, it is our responsibility to stop service by calling stopSelf() or stopService()

    IntentService

    • It performs long task usually no communication with main thread if communication is needed then it is done by Handler or BroadcastReceiver
    • Invoke via Intent
    • Triggered from Main Thread
    • Runs on the separate thread
    • Can't run the task in parallel and multiple intents are Queued on the same worker thread.
    0 讨论(0)
  • 2020-11-22 04:01

    If someone can show me an example of something that you can be done with an IntentService and can not be done with a service and the other way around.

    IntentService can not be used for Long Time Listening, Like for XMPP Listeners, its a single time operator, do the job and wave goodbye.

    Also it has just one threadworker, but with a trick, you can use it as unlimited.

    0 讨论(0)
  • 2020-11-22 04:06

    Don't reinvent the wheel

    IntentService extends Service class which clearly means that IntentService is intentionally made for same purpose.

    So what is the purpose ?

    `IntentService's purpose is to make our job easier to run background tasks without even worrying about

    • Creation of worker thread

    • Queuing the processing multiple-request one by one (Threading)

    • Destroying the Service

    So NO, Service can do any task which an IntentService would do. If your requirements fall under the above-mentioned criteria, then you don't have to write those logics in the Service class. So don't reinvent the wheel because IntentService is the invented wheel.

    The "Main" difference

    The Service runs on the UI thread while an IntentService runs on a separate thread

    When do you use IntentService?

    When you want to perform multiple background tasks one by one which exists beyond the scope of an Activity then the IntentService is perfect.

    How IntentService is made from Service

    A normal service runs on the UI Thread(Any Android Component type runs on UI thread by default eg Activity, BroadcastReceiver, ContentProvider and Service). If you have to do some work that may take a while to complete then you have to create a thread. In the case of multiple requests, you will have to deal with synchronization. IntentService is given some default implementation which does those tasks for you.
    According to developer page

    1. IntentService creates a Worker Thread

    2. IntentService creates a Work Queue which sends request to onHandleIntent() method one by one

    3. When there is no work then IntentService calls stopSelf() method
    4. Provides default implementation for onBind() method which is null
    5. Default implementation for onStartCommand() which sends Intent request to WorkQueue and eventually to onHandleIntent()
    0 讨论(0)
提交回复
热议问题