2

I am new to Android and am now working on the counter thing using the AsyncTask.

So the thing is I'm having one button and with that button OnClickListener.

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                boolean check;
                check=check_button_status();
                if(check==true){
                rec_forward_task.execute();
                }
                else
                {
                    rec_forward_task.cancel();
                }

            }
        });

So here the rec_forward_task is the class that extends the AsyncTask. The AsyncTask class is here. //

private class CounterForwardTask extends AsyncTask<Void, Integer, Integer>
{
    TextView record_counter_display;
    int rec_counter,count;
    int last_value;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        record_counter_display=(TextView) findViewById(R.id.rec_voice_counter);
        rec_counter=0;
        record_counter_display.setText(String.valueOf(rec_counter));

        count=0;
    }

    public void cancel() {
        // TODO Auto-generated method stub
        onCancelled();
    }

    @Override
    protected Integer doInBackground(Void... params) {
        // TODO Auto-generated method stub
        while(rec_status)
        {
            publishProgress(count);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            last_value=count;
            count=count+1;
        }
        return 1;
    }

    @Override
    protected void onCancelled() {
        // TODO Auto-generated method stub
        record_counter_display.setText(String.valueOf(0));
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                record_counter_display.setText(String.valueOf(count));
            }
        });
    }

    @Override
    protected void onPostExecute(Integer result) {
        // TODO Auto-generated method stub
        record_counter_display.setText(String.valueOf(last_value));
    }

}

I'm making the object for that class in the oncreate method only so now the question is.

When user press first time the counter is starting and displaying in the textview and again pressing that same button the counter progress is stop due to oncancel method is called.but if user again pressing the same button app force closing and exception that u can't start the task which is already started.so what will be the way to perform this kind of operation.Thanks for any reply.

vol7ron
  • 40,809
  • 21
  • 119
  • 172
blackjack
  • 586
  • 2
  • 11
  • 30

3 Answers3

4
if(check==true){
    rec_forward_task = new CounterForwardTask();
    rec_forward_task.execute();
}
else
{
    rec_forward_task.cancel();
}

Instead of instantiating AsyncTask in onCreate instaniate it when you need to start it. Hope this helps.

2

You will have to create a new AsyncTask object. AsyncTasks are meant to run only once.

Check this answer.

Community
  • 1
  • 1
fedepaol
  • 6,834
  • 3
  • 27
  • 34
0

The async task is designed to run only once. But you can run it by creating new instances of the asynctask class. See the answer of Passionate Androiden

Threading rules

There are a few threading rules that must be followed for this class to work properly:

The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.

The task instance must be created on the UI thread. execute(Params...) must be invoked on the UI thread. Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.

The task can be executed only once (an exception will be thrown if a second execution is attempted.)

pls findout more in Developer site

Jashan PJ
  • 4,177
  • 4
  • 27
  • 41