title: Timer in Android not working
body: <p>I'm trying to get an alert box to show up 5 seconds after my application starts on Android. But it's not working and I don't know why. Here is the code:</p>
  
  <pre><code>package com.example.helloandroid;
  
  import java.util.Timer;
  import java.util.TimerTask;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.os.Bundle;
  
  
  
  public class HelloAndroid extends Activity {
  
  public void showalert() {
      /* alert box--------------------------- */
      AlertDialog dialog=new AlertDialog.Builder(HelloAndroid.this).create();
      dialog.setTitle("Test");
      dialog.setMessage("Test");
      dialog.setButton("Test 1",
      new DialogInterface.OnClickListener()
      {
      public void onClick(DialogInterface dialog, int whichButton)
      {
      /* Do some stuff */
      }
      });
      dialog.setButton2("Test 2",
      new DialogInterface.OnClickListener()
      {
      public void onClick(DialogInterface dialog, int whichButton)
      {
      /* Do some stuff */
      }
      });
      dialog.setButton3("Test 3",
      new DialogInterface.OnClickListener()
      {
      public void onClick(DialogInterface dialog, int whichButton)
      {
      /* Do some stuff */
      }
      });
      dialog.show();
      /* -------------------------------------- */
  }
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  
  
      super.onCreate(savedInstanceState);
  
  
      new Timer().schedule(new TimerTask()
      { 
          public void run() 
          { 
              showalert();
          } 
      }, 5000);
  
      setContentView(R.layout.main);
  
  }
  
  }
  </code></pre>
  
  <p>Can anyone see what I am doing wrong? Why doesnt the alert show up...? Thanks for your help!</p>
  
  <p>Best whishes,
  Michael</p>
  
  <p><strong>EDIT (This is the working code after some modifying):</strong></p>
  
  <pre><code>package com.example.helloandroid;
  
  import java.util.Timer;
  import java.util.TimerTask;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.os.Handler;
  
  
  
  public class HelloAndroid extends Activity {
  
      public void showalert() {
          /* alert box--------------------------- */
          AlertDialog dialog=new AlertDialog.Builder(HelloAndroid.this).create();
          dialog.setTitle("Test");
          dialog.setMessage("Test");
          dialog.setButton("Test 1",
          new DialogInterface.OnClickListener()
          {
          public void onClick(DialogInterface dialog, int whichButton)
          {
          /* Do some stuff */
          }
          });
          dialog.setButton2("Test 2",
          new DialogInterface.OnClickListener()
          {
          public void onClick(DialogInterface dialog, int whichButton)
          {
          /* Do some stuff */
          }
          });
          dialog.setButton3("Test 3",
          new DialogInterface.OnClickListener()
          {
          public void onClick(DialogInterface dialog, int whichButton)
          {
          /* Do some stuff */
          }
          });
          dialog.show();
          /* -------------------------------------- */
      }
  
      public final void timerAlert(){ 
  
          Handler handler = new Handler();
          handler.postDelayed(new Runnable() {
              public void run() {
                  showalert();
              }
          }, 5000);
  
      }
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          timerAlert();
      }
  }
  </code></pre>