Android wait AsyncTask to finish

前端 未结 4 834
忘掉有多难
忘掉有多难 2020-12-04 15:47

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

相关标签:
4条回答
  • 2020-12-04 16:14

    Rafiq's response did not work for me - the app hung. I think the reason has to do with the nature of isCancelled(): "Returns true if this task was cancelled before it completed normally." If the task completes normally (i.e. is not cancelled) then while(!task.isCancelled()) { } will loop forever.

    To solve this create a Boolean flag that you instatiate to false and then flip to true in task.onPostExecute(). Then do while(!flag) { } before switching Activities. Additionally, if you'd like to give the main thread a 'break' to let the AsyncTask process a little faster, you can do try this:

    while (!flag) {
        try { Thread.sleep(100); }
        catch (InterruptedException e) { e.printStackTrace(); }
    }
    

    It seems to be working well for me.

    0 讨论(0)
  • 2020-12-04 16:22

    try using

    if (AppHelper.isOnline(this))
            {
                while(!task.isCancelled()){
                   // waiting until finished protected String[] doInBackground(Void... params)          
                  } 
                intent = new Intent(this, OrdineCreaActivity.class);
                this.startActivityForResult(intent, R.id.buttonPagamenti);
            }    
    

    For more information read http://developer.android.com/reference/android/os/AsyncTask.html

    0 讨论(0)
  • 2020-12-04 16:27

    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.

    0 讨论(0)
  • 2020-12-04 16:27
    intent = new Intent(this, OrdineCreaActivity.class);
    context.startActivityForResult(intent, R.id.buttonPagamenti);
    

    Write the above lines in onPostExecute() of you AysncTask. Because if we are using AsyncTask it wont wait there until the task complete.

    0 讨论(0)
提交回复
热议问题