djkevino
djkevino

Reputation: 364

Java Async wait for finish

Ho do i get my function to wait for being done with it's async task before returning the variable?

public boolean CheckOnline(){
    OnlineAsyncTask onlinetsk = new OnlineAsyncTask();
    onlinetsk.execute();
    return Online;
} 

Upvotes: 0

Views: 1971

Answers (2)

Fahim
Fahim

Reputation: 12358

String str_result= new OnlineAsyncTask().execute().get();

This will make it wait till it returns the value

Upvotes: 0

laalto
laalto

Reputation: 152787

You can call get() to wait for the async task to complete and retrieve the result.

However that defeats the purpose of an async task - it's no longer asynchronous. Consider redesigning your app so that you don't need to wait for a result. Instead e.g. use a callback interface to notify that the async task has finished and a result is available.

Upvotes: 2

Related Questions