Search...

Showing posts with label TimerTask. Show all posts
Showing posts with label TimerTask. Show all posts

Tuesday, April 24, 2012

How to use TimerTask in Android?



public class Test extends Activity {
    /** Called when the activity is first created. */
Timer myTimer=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        MyTimerTask myTask = new MyTimerTask();
        myTimer = new Timer();
        myTimer.schedule(myTask, 3000, 1500);
       
        Button b=(Button)findViewById(R.id.button1);
     
        b.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

myTimer.cancel();
System.out.println("Cancel the timer...");

}
});
       
     
       
    }
   
   
    class MyTimerTask extends TimerTask {
   public void run() {
   // ERROR
     System.out.println("TimerTask is running......");
   }
  }
}