• Android实现定时器的方法


    package com.lstech.app;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.widget.TextView;
    
    /**
     * handler定时器
     * 
     * 
     */
    public class HandlerActivity extends Activity {
        TextView tvShow;
        private int i = 0;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tvShow = (TextView) findViewById(R.id.tv_show);
            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...");
                    }
                }
            }
        }
    }

    二、Handler的postDelyed方法

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

    三、Handler + Timer + TimerTask

    package com.lstech.app;
    
    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;
    
    /**
     * 定时器实现
     *  * 
     */
    public class TimerActivity extends Activity {
        TextView tvShow;
        private int i = 0;
        private int TIME = 1000;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tvShow = (TextView) findViewById(R.id.tv_show);
            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);
            }
        };
    }
  • 相关阅读:
    linux 安装Python3
    MYSQL 字符集设置(终端的字符集)
    Linux LVM Logical Volume Management 逻辑卷的管理
    oracle 重命名和重定位数据文件(Oracle Renaming and Relocating Datafiles)
    Azkaban编译
    基于hive的transform实现自定义分隔符数据导出
    MapReduce优化设置
    hive.groupby.skewindata环境变量与负载均衡
    hive的基本操作
    Shell 数组的定义和使用
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6394341.html
Copyright © 2020-2023  润新知