android中的定时器

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脚本之家

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值