title: Android: how can i create a timer for 1 second
body: <p>ok i am developing an andoird app which is the game simon, i encountered a problem that when i move from the menu to the game.
  i want the game will play 3 sounds at the beggining of the game which is selected randomly
  and the app to play them 1 after another in 1 sec delay, but the problem is the app before the game backgound is loaded the sounds all play at the same time.. shortly everything doesn't work and i am desprate. 
  in order to use the buttons, i created a class called column which contains the button from the view and the sound it makes and use the method play to activate the sound and change the image.
  the class Game for the logic of the game
  here is the code:</p>
  
  <p>Column Class:</p>
  
  <pre><code>package com.example.thegreatsimon;
  import android.content.Context;
  import android.content.Intent;
  import android.content.res.AssetManager;
  import android.media.AudioManager;
  import android.media.MediaPlayer;
  import android.media.SoundPool;
  import android.os.Handler;
  import android.os.SystemClock;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageButton;
  
  public class Column {
  
  ImageButton button;
  SoundPool sound;
  int soundId;    
  int srcColor,lightColor;
  Context con;
  public Column(ImageButton button,Context con)
  {
      sound=new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
      this.button=button;
      switch (button.getId())
      {   
      case R.id.ibBlue:
      {
          soundId=sound.load(con,R.raw.a ,1);
          srcColor=R.drawable.blue;
          lightColor=R.drawable.lightblue;
      } break;
      case R.id.ibGreen:
      {
          soundId=sound.load(con,R.raw.b,1);
          srcColor=R.drawable.green;
          lightColor=R.drawable.lightgreen;
      } break;
      case R.id.ibRed:
      {
          soundId=sound.load(con,R.raw.c ,1);
          srcColor=R.drawable.red;
          lightColor=R.drawable.lightred;
      } break;
      case R.id.ibYellow:
      {
          soundId=sound.load(con,R.raw.d ,1);
          srcColor=R.drawable.yellow;
          lightColor=R.drawable.lightyellow;
      } break;
      }   
      button.setOnClickListener(new Listen());
  }   
  
  class Listen implements OnClickListener
  {
      @Override
      public void onClick(final View v) 
      {
          play();
      }   
  }
  
  void play()
  {
      sound.play(soundId, 1, 1, 0, 0, 1)  ;
      button.setBackgroundResource(lightColor);
      Handler handler = new Handler();    
      handler.postDelayed(new Runnable() 
      {
          @Override
          public void run() 
          {
              button.setBackgroundResource(srcColor);
          }
      }, 300);
  
  }
  
  }   
  </code></pre>
  
  <p>The Game Class:</p>
  
  <pre><code>package com.example.thegreatsimon;
  import java.util.ArrayList;
  import java.util.Random;
  import java.util.Timer;
  import java.util.TimerTask;
  import android.media.AudioManager;
  import android.media.MediaPlayer;
  import android.media.SoundPool;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.SystemClock;
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.view.Menu;
  import android.widget.Button;
  import android.widget.ImageButton;
  
  public class Game extends Activity {
  
  Column bYellow,bRed,bGreen,bBlue;
  SoundPool a=new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
  int diff;
  int life=3;
  Random rand= new Random();
  Handler hand = new Handler();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      diff = Settings.difficulty;
      switch(diff)
      {
      case(0):
          setContentView(R.layout.gameeasy);
      break;
      case(1):
          setContentView(R.layout.game);
      bYellow=new Column((ImageButton)findViewById(R.id.ibYellow),getApplicationContext());
      break;
      case(2):
          setContentView(R.layout.game);
      bYellow=new      Column((ImageButton)findViewById(R.id.ibYellow),getApplicationContext());
      break;
      }
      bRed=new Column((ImageButton)findViewById(R.id.ibRed),getApplicationContext());
      bGreen=new Column((ImageButton)findViewById(R.id.ibGreen),getApplicationContext());
      bBlue=new Column((ImageButton)findViewById(R.id.ibBlue),getApplicationContext());
      EasyGame();
  }
  
  
  public boolean EasyGame()
  {
      int r;
      ArrayList list = new ArrayList();
      boolean proceed;
      for(int i=0;i&lt;3;i++)
      {
          r=rand.nextInt(3);
          switch(r)
          {
          case 0:
          {
              list.add(bRed);
              Sleep(bRed);                
          }break;
          case 1:
          {
              list.add(bGreen);
              Sleep(bGreen);
          }break;
          case 2:
          {
              list.add(bBlue);
              Sleep(bBlue);
          }break;
          }
      }
      proceed=UserPlay();
  
      /*for(int i=0;i&lt;2;i++)
      {
          r=rand.nextInt(3);
          switch(r)
          {
          case 0:
              list.add(bRed);
              break;
          case 1:
              list.add(bGreen);
              break;
          case 2:
              list.add(bBlue);
              break;
          }
          for(int j=0;j&lt;list.size();j++)
          {
              ((Column)list.get(j)).play();
              Sleep();
          }
          proceed=UserPlay();
      }*/
  
      return true;
  }
  
  public void MediumGame()
  {
  
  }
  public void HardGame()
  {
  
  }
  
  
  
      public void Sleep(final Column c)
  {
      Handler handler = new Handler();    
      handler.postDelayed(new Runnable() {
      @Override
                  public void run() {
                     c.play();
                  }
              }, 1000);
  }
  
  
  
  public boolean UserPlay()
  {
      return true;
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
  }
  
  }
  </code></pre>