Interval / delay / Timer / wait for times. / thread / interval


Timer Class
A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.

Timer timer = new Timer();

timer.schedule(
new TimerTask() {
   
@Override
   
public void run() {
       
//do whatever task you want to do after 10 seconds
       
mediaPlayer.stop();
    }
},
10000);



Handler Class


Best

Handlers are the best way of communication between the background and UI thread. Generally, Handlers are associated with message Queue of a Thread and they are used to send messages and runnable to the Message. Handler Used to communicate between the UI and Background thread.

Handler handler = new Handler();

handler.postDelayed(new Runnable() {
    @Override    public void run() {
        //Do wharever you want        i++;
        TV_second.setText("sec : "+i);
    }
}, 1000);
looping with Handler and delay
final Handler handler = new Handler();
Runnable task = new Runnable() {
@Override
public void run() {
i++;
TV_second.setText("sec : "+i);
handler.postDelayed(this,1000);
}
};
handler.post(task);




মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

API (Application Programming Interface) .

Add Gif file in Android xml file