title: How to use AsyncTask on Android
body: <p>I'm trying to use the <code>AsyncTask</code> to run a thread in the background, it is <code>timer that restarts every 20 seconds</code>, here is my code but I can not get it to work properly, can someone tell me what my error is?</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private Boolean  statusKeyboard = false;
  private EditText mTextoEditor1;
  private Button mBotonSend;
  private TextView mDumpTextView;
  private MiTareaAsincrona tarea1;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate( savedInstanceState );
      setContentView( R.layout.activity_main );
      Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
      setSupportActionBar( toolbar );
  
      mBotonSend = findViewById(R.id.bt2_SendButton);
      mDumpTextView = (TextView) findViewById(R.id.tv1_ReadValues);
      mTextoEditor1 = (EditText) findViewById(R.id.et1_WriteValues);
  
      mBotonSend.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              String message = mTextoEditor1.getText().toString();
              if (mTextoEditor1.getText().toString().trim().equals("")) {
                  Toast.makeText(MainActivity.this, "Por favar Ingresar al texto...", Toast.LENGTH_SHORT).show();
              } else {                                
                  mDumpTextView.append( message + "\r\n" );
              }
              mTextoEditor1.setText("");
          }
      });
  
      tarea1 = new MiTareaAsincrona();
      tarea1.execute();
  }
  
  private class MiTareaAsincrona extends AsyncTask&lt;Void, Integer, Boolean&gt; {
      @Override
      protected Boolean doInBackground(Void... params) {
          setupTimer(20);
          return true;
      }
  
      @Override
      protected void onPostExecute(Boolean result) {
          if(result)
              Toast.makeText(MainActivity.this, "Timer finalizado!", Toast.LENGTH_SHORT).show();
      }
  
      @Override
      protected void onCancelled() { }
  }
  
  private void setupTimer(Integer milisegundos) {
      new CountDownTimer( milisegundos, 1000 ) {
          public void onTick(long millisUntilFinished) {
              //mDumpTextView.setText( "seconds: " + millisUntilFinished / 1000 );
          }
  
          public void onFinish() {
              mDumpTextView.setText( "done!" );
              start();
          }
      }.start();
    }
  }
  </code></pre>