android 计时器 线程

        计时器好多东西,自己整了个,可以记录当前时间往后走了多少面,每隔1S刷新。

1.线程的代码(具体的时间可以自己保存格式,这里只是举一个例子):

//refresh time's thread
    class RefreshTimerThread extends Thread{

        @Override
        public void run() {
            super.run();
            try{
                while (true){
                    Thread.sleep(1000);
                    long currentTimeMiles = System.currentTimeMillis();
                    long timeCost = currentTimeMiles - mStartCatchExceptionTime  + 16 * 60 * 60 * 1000;
                    CharSequence timeCostString = DateFormat.format("HH:mm:ss", timeCost);
                    Message message = mHandlerRefreshView.obtainMessage();
                    message.what = NOTIFY_TIME_TEXT_REFRESH;
                    message.obj = timeCostString;
                    mHandlerRefreshView.sendMessage(message);
                }
            }catch (InterruptedException e){
                Log.i("blb", "------timer refresh interrupted");
            }
        }
    }

java的时间默认是从1970:08:00开始的,所以这里需要加上16个小时的毫秒数来保证计时从0开始。

2.handler 的代码就不用写了,哪都有代码。

3.使用方法:

     开始计时:

 mRefreshTimerThread = new RefreshTimerThread();
                mStartCatchExceptionTime = System.currentTimeMillis();

                mRefreshTimerThread.start();

     结束计时:

mRefreshTimerThread.interrupt();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值