Android Timer

Keywords: Attribute

Define a timer and define a TimerTask to handle handler events

Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        @Override
         public void run() {
                                Thread thread = new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Message message = new Message();
                                        message.what = 2;
                                        mHandler.sendMessage(message);
                                    }
                                });
                                thread.start();
                            }
    };     

## Turn on a timer

 timer.schedule(task,200,10000);

Explain:
The first parameter is the task to be done by the timer, where handler messages are processed.
The second parameter is what event does the task. Here we write 200 ms, that is, after 200 ms, timer begins to process the task.
The third parameter is the time interval between tasks, that is, tasks are executed every 10 seconds. If not written, the timer only executes tasks once.

Timer Request Period

If the timer is not destroyed, it will be executed all the time, but if the timer is executed all the time, then the program will probably collapse, so it is said that the timer should be closed in time when it is used up.
The opening of the timer is usually written in onStart.
Timer destruction is written in onStop

Destroy timers

timer.cancel();

Execute this sentence when you leave the interface, then the timer will not be executed, and when you enter the interface, the timer will be executed in onStart and the timer will be turned on.
Note: The opening and destruction of timer are not necessarily defined in onStart() or onDestroy() methods.

A Timer Code

 touchtimer = new Timer();
                        touchtimer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                Thread thread = new Thread(new Runnable() { //Define a thread
                                    @Override
                                    public void run() {
                                        Message message = new Message();//Define a message
                                        message.what = 2;                 //Customize what attribute of a message (distinguishing different messages)
                                        mHandler.sendMessage(message); //send message
                                    }
                                });
                                thread.start();                  //Open threads
                            }
                        },300,200);
 public Handler mHandler = new Handler() {       //Define a Handler and process messages
        public void handleMessage(Message msg) { //Accept and process messages sent by threads
            switch (msg.what) {                 //what identifies messages sent by threads
                case 2:                         //According to what, different event methods are executed, and only one what is defined here
                   //Execute defined event or event handling methods;   
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        };
        protected void onDestroy() {                 //Destroy timer
            super.onDestroy();
            timer.cancel();
           } 

Posted by tamayo on Wed, 31 Jul 2019 09:41:47 -0700