• Android学习笔记(四) 定时器Timer


     

    Android考虑到线程安全问题,不允许在线程中执行UI线程。

    所以在线程中不允许有UI操作

    可以利用Handler机制来接收Timer每隔一秒发出的信息,也可以直接利用handler机制的

    1、方法一:Handler+Thread

    package com.example.yuyin_lixian;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    
         
    /**
     * handler定时器
     *
     * @author Smalt
     *
     */
    public class MainActivity extends Activity {
        TextView tvShow;
        private int i = 0;
         
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tvShow = (TextView) findViewById(R.id.textView1);
            new Thread(new ThreadShow()).start();
        }
         
        // handler类接收数据
        Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    tvShow.setText(Integer.toString(i++));
                    System.out.println("receive....");
                }
            };
        };
         
        // 线程类
        class ThreadShow implements Runnable {
         
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (true) {
                    try {
                        Thread.sleep(1000);   //每隔一秒执行一次
                        Message msg = new Message();
                        msg.what = 1;
                        handler.sendMessage(msg);
                        System.out.println("send...");
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println("thread error...");
                    }
                }
            }
        }
    }

    2、方法二:Handler类自带的postDelyed

    其中使用PostDelayed方法,1秒后调用此Runnable对象
    handler.postDelayed(runnable, 1000); 实际上也就实现了一个1s的一个定时器

    如果想要关闭此定时器,可以这样操作
    handler.removeCallbacks(runnable);

    package com.example.yuyin_lixian;
    
    import android.app.Activity;  
    import android.os.Bundle;  
    import android.os.Handler;  
    import android.widget.TextView;  
      
    /** 
     * handler定时器使用postDelyed实现 
     *  
     * @author Smalt 
     *  
     */  
    public class MainActivity extends Activity {  
        TextView tvShow;  
        private int i = 0;  
        private int TIME = 1000;  
      
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            tvShow = (TextView) findViewById(R.id.textView1);  
            handler.postDelayed(runnable, TIME); //每隔1s执行  
        }  
      
        Handler handler = new Handler();  
        Runnable runnable = new Runnable() {  
      
            @Override  
            public void run() {  
                // handler自带方法实现定时器  
                try {  
                    handler.postDelayed(this, TIME);  
                    tvShow.setText(Integer.toString(i++));  
                    System.out.println("do...");  
                } catch (Exception e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                    System.out.println("exception...");  
                }  
            }  
        };  
      
    }

    3、方法二:Handler+Timer+TimerTask

    package com.example.yuyin_lixian;
    
    import java.util.Timer;  
    import java.util.TimerTask;  
      
    import android.app.Activity;  
    import android.os.Bundle;  
    import android.os.Handler;  
    import android.os.Message;  
    import android.widget.TextView;  
      
    /** 
     * 定时器实现:Handler+Timer+TimerTask 
     *  
     * @author Smalt 
     *  
     */  
    public class MainActivity extends Activity {  
        TextView tvShow;  
        private int i = 0;  
        private int TIME = 1000;  
      
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            tvShow = (TextView) findViewById(R.id.textView1);  
            timer.schedule(task, 1000, 1000); // 1s后执行task,经过1s再次执行  
        }  
      
        Handler handler = new Handler() {  
            public void handleMessage(Message msg) {  
                if (msg.what == 1) {  
                    tvShow.setText(Integer.toString(i++));  
                }  
                super.handleMessage(msg);  
            };  
        };  
        Timer timer = new Timer();  
        TimerTask task = new TimerTask() {  
      
            @Override  
            public void run() {  
                // 需要做的事:发送消息  
                Message message = new Message();  
                message.what = 1;  
                handler.sendMessage(message);  
            }  
        };  
    }
  • 相关阅读:
    Gecko Bootloader的介绍(Silicon Labs)【一】
    使用模板新建ZigBee工程的方法
    代码控制ZigBee网络密钥的生成
    Ubuntu20编译最新版Android源码教程
    C和C++常用代码片段整理
    Java易错的知识点整理
    仿IntelliJ Darcula的Swing主题FlatLaf使用方法
    PuTTYTabManager汉化版
    WinSCP整合SecureCRT打开终端
    异想家博客图片批量压缩程序
  • 原文地址:https://www.cnblogs.com/Kevin127/p/5022146.html
Copyright © 2020-2023  润新知