Service vs IntentService

Asked 2 years ago, Updated 2 years ago, 82 views

It's not possible with the service, but can you tell me an example of what you can do with IntentService (including the opposite situation)?

I understand that one IntentService runs on one independent thread, and the Service does not. So I think running the service within your own thread is the same as running IntentService. No?

I would appreciate it if you could answer the two questions.

android multithreading android-intentservice android-service

2022-09-22 11:37

1 Answers

Tejas Lagvankar once wrote a good document on this subject. Below are the key differences between Service and IntentService.

When to Use

Service is used for non-UI-related tasks and should not take long. If you need to perform long tasks, you must use threads within the service.

IntentService is usually used for long operations and usually does not interact with the main thread. If you need to interact with the main thread, you can use the main thread handler or broadcast int. It is also used in situations where callback is required (intent to start a specific task).

How to start

Service is initiated by the startService() method call.

IntentService is started using Intent, creating a new task thread, and the onHandleIntent() method is invoked within this thread.

starting position

Operation Location

Servie works in the background and on the main thread of the app.

IntentService works on each task thread.

Restrictions/Disadvantages

Servie can block the main thread of the app.

IntentService does not work in parallel. So, the intents that are tied up in a chain will enter the message queue of the task thread and run sequentially.

When to stop

If Service has been implemented, it is up to you to stop the service when all tasks are completed. Through the stopSelf() or stopService() method. (If you just want to provide binding, you don't need to implement this method.))

IntentService stops the service after all requests are processed. So you don't have to call the stopSelf() method.


2022-09-22 11:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.