开发者社区> 问答> 正文

android 定时器刚开始计时时有几秒延时

刚开始有大概2秒延时,怎么回事

 final TextView timeout = (TextView)grid.findViewById(R.id.timeout);
 final ImageButton tock_on = (ImageButton) grid.findViewById(R.id.tock_on);
  tock_on.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
 timeout.setText("00:00:00");
                            stepTimeHandler = new Handler();
                            startTime = System.currentTimeMillis();
                            mTicker = new Runnable() {
                            public void run() {
                                String content = showTimeCount(System.currentTimeMillis() - startTime);
                                timeout.setText(content);
                                long now = SystemClock.uptimeMillis();
                                long next = now + (1000 - now % 1000);
                                stepTimeHandler.postAtTime(mTicker, next);
                            }
                    };
                    //启动计时线程,定时更新
                    mTicker.run();
                                         }
            });

展开
收起
爵霸 2016-06-20 10:11:00 2321 0
1 条回答
写回答
取消 提交回答
  • 第一次执行时间也就是new runnalbe和执行showtimecount的时间,应该不至于这么慢。不过如果是text偶尔跳动2秒是有可能的。先不说系统延时之类的。就从逻辑分析是有可能的

     stepTimeHandler = new Handler();
                                startTime = System.currentTimeMillis();
                                mTicker = new Runnable() {
                                public void run() {
                                    String content = showTimeCount(System.currentTimeMillis() - startTime);System.currentTimeMillis() 是999毫秒,相差不到1秒,显示应该还是00:00:00
                                    timeout.setText(content);
                                    long now = SystemClock.uptimeMillis();//这里比如SystemClock.uptimeMillis() 刚好过了当前秒了,是1秒001毫秒
                                    long next = now + (1000 - now % 1000);//到这里下次执行时间就是2秒的时候执行
                                    stepTimeHandler.postAtTime(mTicker, next);//再加上post延时,下次显示就是00:00:02了
                                }
    2019-07-17 19:43:41
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载