Lesson 90. AsyncTask. Status - statuses of the task
Android Lessons

Lesson 90. AsyncTask. Status – statuses of the task


In this lesson:

– read the statuses of the task

We can always determine the status of the task at hand. Statuses are used for this purpose. There are only three of them:

PENDING – task not yet started

RUNNING – task in work

FINISHED – the onPostExecute method worked, that is, the task was successfully completed

In principle, the names are all clear, but let’s look and see for yourself. In addition, it is not very clear what the status of the task is if we cancel it with the cancel method.

Consider the example.

Let’s create a project:

Project name: P0901_AsyncTaskStatus
Build Target: Android 2.3.3
Application name: AsyncTaskStatus
Package name: en.startandroid.develop.p0901asynctaskstatus
Create Activity: MainActivity

strings.xml:



	AsyncTaskStatus
	Start
	Status

main.xml:



	
	
	
	

at the button Start run the task, by the button Status we will display its status.

MainActivity.java:

package ru.startandroid.develop.p0901asynctaskstatus;

import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

  MyTask mt;
  TextView tvInfo;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tvInfo = (TextView) findViewById(R.id.tvInfo);
  }

  public void onclick(View v) {
    switch (v.getId()) {
    case R.id.btnStart:
      startTask();
      break;
    case R.id.btnStatus:
      showStatus();
      break;
    default:
      break;
    }
  }

  private void startTask() {
    mt = new MyTask();
  }

  private void showStatus() {
    if (mt != null)
      Toast.makeText(this, mt.getStatus().toString(), Toast.LENGTH_SHORT).show();
  }


  class MyTask extends AsyncTask {
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      tvInfo.setText("Begin");
    }

    @Override
    protected Void doInBackground(Void... params) {
      try {
        for (int i = 0; i < 5; i++) {
if (isCancelled()) return null;
          TimeUnit.SECONDS.sleep(1);
        }
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return null;
    }

    @Override
    protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      tvInfo.setText("End");
    }
    
    @Override
    protected void onCancelled() {
      super.onCancelled();
      tvInfo.setText("Cancel");
    }
  }
}

When the button is pressed Start the startTask method works. We are just creating a new task in it, but we are not running it yet.

When the button is pressed Status read and display the status of the task. We use the getStatus method.

The task itself is simple, we drive in a pause cycle.

We'll save everything and run it.

press Start - the task was created.

press Status

status = PENDING. That's right, the task is not yet started.

rewrite the method startTask:

  private void startTask() {
    mt = new MyTask();
    mt.execute();
  }

Now we will create the task and start it.

Save, run. press Start. The task started to work.

press StatusWhile the task is running

status = RUNNING. Tasks at work.

After the task is completed (End on screen), click Status

status = FINISHED. Job completed successfully, onPostExecute method completed.

Now let's see what the status will be if you cancel the task. rewrite startTask:

  private void startTask() {
    mt = new MyTask();
    mt.execute();
    mt.cancel(false);
  }

After a while, we cancel the task immediately after it starts.

Save, launch the application. press Start.

Job was canceled. press Status

Status for some reason RUNNINGAs if the task at work. Not particularly logical, of course, but here is such a feature of implementation. Why another CANCELED status could not be entered - I don't know. Android developers are more visible.

How, then, to distinguish whether a task is started or canceled? You can use the isCancelled method. rewrite showStatus for example:

  private void showStatus() {
    if (mt != null)
      if (mt.isCancelled())
        Toast.makeText(this, "CANCELLED", Toast.LENGTH_SHORT).show();
      else
        Toast.makeText(this, mt.getStatus().toString(), Toast.LENGTH_SHORT).show();
  }

Save, launch the application. press Start, The job is canceled. press Status

The job was canceled, so isCancelled returned true, and we returned the message.

In the next lesson:

- keep in touch with AsyncTask when you rotate the screen




Discuss in the forum [20 replies]

Leave a Reply

Your email address will not be published. Required fields are marked *