android中的定时器

时间:2023-02-05 23:23:29

Thread的sleep(long)方法

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        new Thread(runnable).start();
    }

    private int looper = 0;
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.d("ThreadAct", ++looper + "");
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        runnable = null;
    }

Handler的postDelayed(Runnable, long)方法

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        Log.d("HandlerAct","Thread.name:"+Thread.currentThread().getName());
        runHandler.postDelayed(runnable,2000);
    }
    private Handler runHandler  = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    private int looper = 0;
    Runnable runnable = new Runnable(){
        @Override
        public void run() {
            //TODO something
            Log.d("HandlerAct",++looper+"");
            Log.d("HandlerAct","Thread.name:"+Thread.currentThread().getName());
            runHandler.postDelayed(this,2000);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        runHandler.removeCallbacks(runnable);//停止
    }

Timer与TimerTask结合的方法

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        timer.schedule(task,1000,2000);
    }

    private int looper = 0; 

    Timer timer = new Timer( ); 

    TimerTask task = new TimerTask( ) {
        public void run ( ) {
            timerHandler.sendEmptyMessage(0);
        }
    };

    Handler timerHandler = new Handler( ) {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.d("TimerAct",++looper+"");
        }
    };

    protected void onDestroy ( ) {
        if (timer != null) {
            timer.cancel( );
            timer = null;
        }
        super.onDestroy( );
    }

使用schedule方法,1秒后调用此task对象,然后每2秒执行一次

timer.schedule(task,1000,2000);

Android中的定时器Timer、AlarmManager、CountDownTimer的使用 - Mirhunana - 博客频道 - CSDN.NET

ScheduledThreadPoolExecutor

利用ScheduledThreadPoolExecutor定时执行任务:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        threadPool.scheduleAtFixedRate(task,0,2, TimeUnit.SECONDS);
    }

    private ScheduledThreadPoolExecutor threadPool = new ScheduledThreadPoolExecutor(5);

    private int looper = 0;

    TimerTask task = new TimerTask( ) {
        public void run ( ) {
            timerHandler.sendEmptyMessage(0);
        }
    };

    final Handler timerHandler = new Handler( ) {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.d("ExecutorAct",++looper+"");
        }
    };

Java ScheduledThreadPoolExecutor延迟或周期性执行任务 - ImportNew
http://www.importnew.com/7276.html
Android定时器,推荐ScheduledThreadPoolExecutor - 山不在高,有金则名 - 博客频道 - CSDN.NET
http://blog.csdn.net/gaojinshan/article/details/17956055
利用ScheduledThreadPoolExecutor定时执行任务 - 幻年,时光。 - 博客频道 - CSDN.NET
http://blog.csdn.net/kazeik/article/details/8545049

深度解析Java8 – ScheduledThreadPoolExecutor源码解析 - 为程序员服务
http://ju.outofmemory.cn/entry/99456
ScheduledThreadPoolExecutor实现原理 - - ITeye技术网站
http://freish.iteye.com/blog/1766960
Java Timer和ScheduledThreadPoolExecutor体会和发现的问题明明如月新浪博客
http://blog.sina.com.cn/s/blog_9707fac30101i5n5.html

CountDownTimer倒计时

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        timer.start();
    }
    private int looper = 0;
    private CountDownTimer timer = new CountDownTimer(10000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            Log.d("CountdownTimerAct",++looper+"");
        }

        @Override
        public void onFinish() {
            Log.d("CountdownTimerAct","onFinish");
            Log.d("CountdownTimerAct",++looper+"");
        }
    };

Android CountDownTimer倒计时器的使用 - 拿铁不懂绿茶 - 博客频道 - CSDN.NET
使用CountDownTimer类轻松实现倒计时功能Android脚本之家