hi all i have a problem i want to make the UI thread to wait untill a service has been finished can any one tell me how to acheive this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m going to assume you already have the service setup correctly for communicating with your Activity.
You can use a
Semaphoreto block the UI thread while the service is executing. There are many ways to do this, but for sake of simplicity, thisSemaphorecould be a static field in a utility class.In your UI Thread you would do something like:
UtilityClass.LOCK.acquire()In your service, when done you would release the lock:
UtilityClass.LOCK.release()Keep in mind that a few more checks might be necessary to ensure there are no bugs in this approach. You will want to have a look at the
Semaphoredocumentation. For example, before releasing a permit you might want to check that there are zeroavailablePermits()I think that answers you question, but please be warned that blocking on the UI thread will cause the app to become unresponsive. In other words, this approach is not best practice and should be avoided.