1

I am trying to wait for a background process to finish and then do some stuff accordingly after it finishes.

Basically I have a class TwitterActivity and an inner class CheckInternetConnection which extends AsyncTask. I have also a button mSignin and I set the event handling for it. In addition I have also a boolean hasInternet.

My aim is when the mSignin button will be pressed I will call CheckInternetConnection. This is supposed to update my boolean value hasInternet. Then accordingly I expect to do some stuffs.

But I want exactly to do inside onClick() method.

Is there any way how to achieve it? Thanks.

public class TwitterActivity extends Activity 
{
    private boolean hasInternet = false;
    private Button mSignin;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_twitter);

        mSignin = (Button)findViewById(R.id.login_id);
        mSignin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                new CheckInternetConnection().execute();

                if(hasInternet)
                    //do some stuff
                else
                    //do some other stuff
            }
        });
    } 

    class CheckInternetConnection extends AsyncTask<Void, Void, Boolean>{
        @Override
        protected void onPostExecute(Boolean result){
            if(result)
                hasInternet = true;
            else
                hasInternet = false;
        }
        @Override
        protected Boolean doInBackground(Void... params) {
            return true;
        }
    }
}
user3764893
  • 697
  • 5
  • 16
  • 33

3 Answers3

1

There are ways how you can wait for an AsyncTask to finish, but I don't think you wanna do that because it will block the UI Thread. Instead, move that check to onPostExecute. You can also already check for a connection when the button gets clicked.

public class TwitterActivity extends Activity 
{
    private boolean hasInternet = false;
    private Button mSignin;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_twitter);

        mSignin = (Button)findViewById(R.id.login_id);
        mSignin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                checkInternet();
            }
        });
    } 

    private void checkInternet(){
        if(hasInternet)
            //do some stuff
        else
            new CheckInternetConnection().execute();
    }

    class CheckInternetConnection extends AsyncTask<Void, Void, Boolean>{
        @Override
        protected void onPostExecute(Boolean result){
            if(result)
                hasInternet = true;
            else
                hasInternet = false;

            checkInternet();
        }
        @Override
        protected Boolean doInBackground(Void... params) {
            return true;
        }
    }
}
SimonSays
  • 10,867
  • 7
  • 44
  • 59
0

The purpose of an asynchronous task is to handle an expensive procedure that won't block the main thread. If this is a quick step, don't make it a separate task, simply do it right then and there.

JBires
  • 441
  • 3
  • 11
  • I know that would make sense but `CheckInternetConnection` must run in a asynchronous task, because otherwise it will give me some error – user3764893 Jun 24 '14 at 17:00
0

One way to do it would be:

public class TwitterActivity extends Activity {

  private boolean hasInternet = false;
  private Button mSignin;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_twitter);

    mSignin = (Button)findViewById(R.id.login_id);
    mSignin.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {



        new AsyncTask<Void, Void, Boolean>() {

          @Override
          protected Boolean doInBackground(Void... params) {
              // TODO: put your async job here
              return true;
          }

          @Override
          protected void onPostExecute(Boolean result){
            hasInternet = result;

            if( hasInternet ) {
              // do some stuff
              // 
            } else {
              // do some other stuff
            }

          }
        }.execute();



      }
    });
  }
}
meeDamian
  • 1,143
  • 2
  • 11
  • 24