0
  1. I have a class called "Class A" a) I have a button called verify b) onClick of verify i call auth.authenticate() which returns boolean c) upon true - i have to call an intent

  2. the authenticate function is in another class (auth is a ref)

  3. authenticate function is as follow ()

boolean authenticate() {

 new AsyncTask<String, String, String>() {      

    preExecute()
    {
       --starting a progress bar--
    }
    doInBackground(String )
    {
        ----  huge authentication code-----
        ------ returns true or false -------isVerified

    }
    onPostExecute(String)
    {
            ---   dismiss progress bar-----------
    }       
    }.execute();
    }

  --------end of AsynTask block----

  synchronized (this) {

        try {
            this.wait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

return isVerified; ---- this should return to MyClass - after async finishes

-- but this is returned before async completes.. actually it is correct - because these are thread-- runs parallel, so to make it work - i have waited till async to complete using that synchronized block -- but that will blocking my main thread - (also no progress bar)

I could call my next activity in post execute success - as i m making a library class..i think should not... please help

}

2 Answers 2

0

You can override onPostExecute() and return your value from there, if the task was cancelled it will not be called.

2
  • but Chis, meanwhile "return isVerified" will run irrespective of onPostExecute return.. May 9, 2012 at 8:37
  • The value returned from doInBackground() is passed in to onPostExecute() this is not directly seen by the rest of your code. See the documentation for AsyncTask. May 9, 2012 at 9:13
0

AsyncTasks are also threads. So you can make a new AuthenticateAsyncTask<Void,Void,Void> class for authentication and with overriding the onPostExecute method you can call a function with the parameter isVerified

For Ex:

boolean isVerified;

// Async Task
class AuthenticateAsyncTask extends AsyncTask<Void, Void, Void>{
    @Override
    protected Void doInBackground(Void... params) {
        // Authentication
        isVerified = returningValue // set your isVerified variable
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // you can here call a function that indicates the authenticaion value is returned
    }

    @Override
    protected void onPreExecute() {}

}

Hope this helps

3
  • Hello Nafiz, thanks for looking into it... Instead of a class as you have written I have a anonymous class in a function called authenticate - authenticate will return true of false without waiting for that async to finish.. thats the problem.. i have synchroized wait on this - so that i can wait till async to finish.. but that will block my thread. May 9, 2012 at 9:25
  • And what about calling that authenticate function inside the doInBackground() function? - Start authenticate AsyncTask - Call authenticate function in doInBackground - when the authenticate function returns, the asyncTask's onPostExecute method get called - And you can wait till the asyncTask's onPostExecute() method gets executen. May 9, 2012 at 9:30
  • what i am looking for is - how to wait for another thread(or async) to complete without blocking the UI thread.. this.wait() will block the thread.. May 9, 2012 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.