CountDownTimer CNTT là một lớp abstarct bạn có thể xem chi tiết CountDownTimer. Hiển thị Toast cho thời gian còn Trong ví dụ này, chúng ta sẽ thấy các bánh mì nướng cho 20seconds (20000 mili giây) Code: public class ToastActivity extends Activity { AlertDialog dialog; static CountDownTimer timer =null; Toast toast; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // creating toast and setting properties toast = new Toast(this); TextView textView=new TextView(this); textView.setTextColor(Color.BLUE); textView.setBackgroundColor(Color.TRANSPARENT); textView.setTextSize(20); textView.setText("This Toast will Display for 20 Seconds in Center of The Screen"); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setView(textView); // Toast Display tTime Settings // Create the CountDownTimer object and implement the 2 methods // show the toast in onTick() method and cancel the toast in onFinish() method // it will show the toast for 20 seconds (20000 milliseconds 1st argument) with interval of 1 second(2nd argument) timer =new CountDownTimer(20000, 1000) { public void onTick(long millisUntilFinished) { toast.show(); } public void onFinish() { toast.cancel(); } }.start(); } } Theo mặc định một bánh mì nướng được hiển thị trong 1 hoặc 2 giây. Chúng tôi có thể tùy chỉnh thime này và có thể hiển thị các bánh mì nướng lâu hơn thời gian 5,10,20 giây vv Chúng tôi sử dụng lớp CountDownTimer cho mục đích này. Chúng tôi cũng có thể hủy bỏ bánh mì nướng của chúng tôi trước khi tine quy định là 20 giây trên một điều kiện cụ thể với mã sau Code: // Cancelling toast on a particular condition if(your conditinal expression) { timer.cancel(); } >> Xem thêm: Khóa học android tại hà nội !