Click here to Skip to main content
14,733,966 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Dear all,

I have been working on a android project, the problem I faced is that I need to wait for asyncTask/service/thread/runnable to finish its job and accept it's result when its done so that the operation continues synchronously, however, the code after the callings of asyncTask/service..continues to execute which I don't need it....I think I really have missed something basic concept..

The code below is what I have tried and I did not incorporated what the asyncTask does...within doInBackground it makes ussd call and automatically enters into while loop to get intercepted message...that there is a ussd interceptor which puts the message on a global shared preference..the while loop searches for this specific message and breaks automatically after my wish is to get this message and continue onto the next statement...

If my problem is clear enough, please help me.
Please feel free if my problem is not clear.


What I have tried:

BackgroundMessageFinder asyncTask =
(BackgroundMessageFinder) new BackgroundMessageFinder(Global.context, this,
new BackgroundMessageFinder.AsyncResponse() {

public void processFinish(String output) {
// isAccountValid = Boolean.parseBoolean((output));
}).execute(Constants.PreferenceKey.UssdSmsMsg, "999", "457", "", "1234");

// try {
try {
String test = asyncTask.get(); // get some result

} catch (InterruptedException e) {
} catch (ExecutionException e) {

// continue after fetching the required message
Updated 5-Jun-17 19:37pm
OriginalGriff 20-May-17 4:15am
Don't "bump" your question: it's rude, arrogant, unnecessary, and doesn't help you get a faster answer. By all means add info, but just editing it to get it back to the top of the "unanswered" list is just saying "I'm more important than anyone else, so deal with *MY* question and ignore the rest".
And so is everybody else, and everybody else's questions. If everyone was this thoughtless and rude, you'd never get an answer to anything, because all we'd be able to see would be 100 pages of idiots bumping all the real questions to page 100+
All you do is annoy people, and get them to deliberately not even look at your question, much less answer it.
Yonathan1111 20-May-17 9:35am
Dear OriginalGriff, I don't know why you took it this way at least you may have the chance to question me for such a case..all I needed is to get a concept not even a piece of code..I did not urge or included words that are not allowed in codeproject rules..I have tried to be polite as possible as I is bizarre that I'm getting such response lately..I have a 7 years of history with codeproject..and getting such response is rude..English is my second language..and I apologize for my poor language you have said I have no intention to get my question on top rather fix the expressions that I used in...thanks..

OriginalGriff 20-May-17 9:41am
My comment was in regard to your "bumping" the question: editing it without any changes in order to move it back to the top of the unanswered list. The system maintains change logs and tells us what changes were made - or in your case not made.
Yonathan1111 20-May-17 12:30pm
Okay..I don't have such experiences for the past 7 years and that is what I am trying to tell you..and if you carefully see the change logs at your side you can find the least I have tried to make corrections on the title..

Starting a process and then blocking all other processes waiting on a result is not the way mobile devices (smartphones, tablets) are meant to be used. It's the wrong UX. If the main thread (also called the UI thread) spawns another thread to do some work, the main thread needs to keep right on working. When the secondary, or spawned, thread finishes, it communicates back to the UI thread with its results.
Like already being mentioned, I would pay a great emphasis on the fact that intertwining your synchronous code, with asynchronous code is a bad and poor approach. The two should be left alone, and should work in their own contexts.

What you are creating is useful, and really very much helpful in the cases where you need some IO operations (such as writing to databases, reading files, downloading from network) etc and that is exactly why AsyncTask and other types are provided, but never intertwine the UI with the background threads. Instead, when something is going on in async, perform the rest of the tasks there — you can set events, handlers and runOnUiThread calls to update the UI as the task progresses.

One more thing, async tasks should be used for IO intensive operations only, for anything that is CPU bound, consider running it as a service so that it runs anytime in the background and can consume CPU, but not intensively.

Finally, I would recommend that you give the Android developer documentation a read, to learn more about what Android platform has to offer for threading: Sending Operations to Multiple Threads | Android Developers[^]

android - how to use runOnUiThread - Stack Overflow[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900