TheLostMind
TheLostMind

Reputation: 36304

How to block main thread until Intent service completes?

Program structure :

Service (running in main thread) {

start intent service 1
start intent service 2

// here I have to wait until both intent services have completed. They don't return any data.
//... some code
}

I looked at a similar question on SOF - Android:: Blocking main thread for worker thread to finish before executing other tasks in main thread, but it didnt' help.

Upvotes: 0

Views: 1013

Answers (2)

Hardik
Hardik

Reputation: 17441

you can not block main thread.intent service not mean to be made for blocking main thread but it work as temp background service that have a particular task and reporting to main thread when task is completed.

so you can not block but you can put other logic that will execute after intent service task completed. (it will notify in broadcast receiver when task completed so you can use broadcast receiver which will called when intent service task completed).

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93569

You don't. If you block the main thread, your phone will appear to be frozen, and eventually the app will be killed or at least prompt the user to kill it. Instead, you want your intent services to return a result to the activity, and for the activity to keep track of which ones have returned. When they both have, it should then do the appropriate processing. But under no circumstances should you block the main thread.

Upvotes: 3

Related Questions