Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android wait AsyncTask to finish

I have a function, AppHelper.isOnline(Context context), I call in various parts of my application to check that a session didn't timeout before making an HTTP request.

public void onClick(View v) {         Intent intent = null;         switch (v.getId()) {          case R.id.buttonPagamenti:             if (AppHelper.isOnline(this))             {                 //here AppHelper.isOnline should have finished it's async task                 intent = new Intent(this, OrdineCreaActivity.class);                 this.startActivityForResult(intent, R.id.buttonPagamenti);             }             break; ... 

Inside AppHelper.isOnline(), I am executing an AsyncTask that logs in, thus making a network request, which can't be run on UI because otherwise I get an exception. I need to wait for it to finish BEFORE resuming with the code inside the if. How can I do this ?

Problem is the activity starts firsts, then the AsyncTask executes, so when the activity expects a valid logged in session, it breaks.

like image 925
max4ever Avatar asked Apr 03 '12 16:04

max4ever


People also ask

How do you wait for an async task to finish?

You will need to call AsyncTask. get() method for getting result back and make wait until doInBackground execution is not complete.

Why AsyncTask is deprecated in Android?

This class was deprecated in API level 30.AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.

How to call AsyncTask in Android?

To start an AsyncTask the following snippet must be present in the MainActivity class : MyTask myTask = new MyTask(); myTask. execute(); In the above snippet we've used a sample classname that extends AsyncTask and execute method is used to start the background thread.

How to cancel Async task Android?

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns.


1 Answers

You have two options:

Either use the AsyncTask's method get(long timeout, TimeUnit unit) like that:

task.get(1000, TimeUnit.MILLISECONDS); 

This will make your main thread wait for the result of the AsyncTask at most 1000 milliseconds (as per @user1028741 comment: actually there is also infinetly waiting method - AsyncTask#get() which might also do the work for you in some cases).

Alternatively you can show a progress dialog in the async task until it finishes. See this thread (No need for me to copy past the code). Basically a progress dialog is shown while the async task runs and is hidden when it finishes.

You have even third option:" if Thread is sufficient for your needs you can just use its join method. However, if the task is taking a long while you will still need to show a progress dialog, otherwise you will get an exception because of the main thread being inactive for too long.

like image 164
Boris Strandjev Avatar answered Sep 19 '22 23:09

Boris Strandjev