0

So I have an Asynctask class to perform the next action 5 seconds after clicking a button.

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            delay = new delayMessageAsyncTask();
            delay.setProgressBar(delaySendProgressWheel);
            delaySendProgressWheel.setProgress(0);
            delaySendProgressWheel.setSpinSpeed(0.21f);
            mButton.setVisibility(ImageView.GONE);
            delaySendProgressWheel.setVisibility(ProgressWheel.VISIBLE);
            delay.execute();

            //performAction();
        }
    });


class delayMessageAsyncTask extends AsyncTask<Void, Integer, Void> {
    int myProgress;
    @Override
    protected void onPreExecute() {
        myProgress = 0;
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void result) {
        edit_text.setText("");
        mButton.setVisibility(ImageView.VISIBLE);
        delaySendProgressWheel.setVisibility(ProgressWheel.GONE);
        msgAdapter.setArrayList(message);
        msgAdapter.notifyDataSetChanged();
        listView.smoothScrollToPosition(msgAdapter.getCount()-1);
        super.onPostExecute(result);
    }

    @Override
    protected Void doInBackground(Void... params) {
        while(myProgress<100){
            myProgress+=20;
            publishProgress(myProgress);
            try {
                Thread.sleep(1000);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        performAction();
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        delaySendProgressWheel.setProgress((float)values[0]);
    }

The thing is, the performAction() is quite complicated since it contains both background thread and UI thread, so I have to put a portion inside doInBackground and update the UI onPostExecute. But as I said only after exactly 5 seconds can the action be performed so it stops for a while before reaching onPostExecute. I have tried moving the performAction() to onPostExecute, but it cause the UI to freeze. Ideally, I want to know if theres any way to stop executing the code after the execute() method in onClick() so I can put the performAction() there.

Kise
  • 2,823
  • 1
  • 24
  • 43

2 Answers2

3

You can add a delay with a handler after the click is pressed:

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      public void run() {
        // exec code here
      }
    }, 5000);
Gary Bak
  • 4,746
  • 4
  • 22
  • 39
1

You can use this:

  Handler mHandler;

  public void postHandler() {
    mHandler = new Handler();
    mHandler.postDelayed(mRunnable, 5000);
  }

  private Runnable mRunnable = new Runnable() {

    @Override
    public void run() {
      /** Do something **/

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

        @Override
        protected Void doInBackground(Void... params) {

            performAction();
            return null;
        }

       }.execute();
    }
  };
Anderson K
  • 5,445
  • 5
  • 35
  • 50
  • If I want to update the progress wheel during that 5 seconds, where should I put the code? Because I want to use Asynctask mainly to show the time left. – Kise Mar 22 '15 at 02:49
  • you can use [CountDownTimer](http://stackoverflow.com/questions/10241633/android-progressbar-countdown) ! – Anderson K Mar 22 '15 at 02:57
  • thank you, I will mark your answer as correct. I just realized that the problem lies in my performAction() method, it still cause the UI freeze no matter what, so I will look into fixing it. – Kise Mar 22 '15 at 03:14