zoukankan      html  css  js  c++  java
  • java的 Timer

    前言

      学习android一段时间了,为了进一步了解android的应用是如何设计开发的,决定详细研究几个开源的android应用。从一些开源应用中吸收点东西,一边进行量的积累,一边探索android的学习研究方向。这里我首先选择了jwood的 Standup Timer 项目。

    Timer

      如果需要定期执行某些任务,可以使用Timer 类,它接受一个TimerTask用于指示需要执行的操作。Timer会在后台开一个线程来进行定期任务处理。在Standup Timer 中使用它来计时,其实在本系列文章中的上一篇:android的Handler 就已经使用了Timer。下面我们来看看Standup Timer里相关的代码:
    Timer
    privatevoid startTimer() {
    Logger.d(
    "Starting a new timer");
    timer
    =new Timer();
    TimerTask updateTimerValuesTask
    =new TimerTask()
    {

    @Override
    publicvoid run() {
    updateTimerValues();

    }

    };
    timer.schedule(updateTimerValuesTask,
    1000, 1000);

    }
     
    updateTimerValues
    protectedsynchronizedvoid updateTimerValues()
    {
    currentIndividualStatusSeconds
    ++;
    if (remainingIndividualSeconds >0)
    {
    remainingIndividualSeconds
    --;
    if (remainingIndividualSeconds == warningTime)
    {
    Logger.d(
    "Playing the bell sound");
    if (SettingActivity.playSounds(this)) {
    //如果等于设定的警告时间且设置允许警告则播放警告铃声
    playWarningSound();
    }
    //if
    } else {
    if (remainingIndividualSeconds ==0) {
    Logger.d(
    "Playing the airhorn sound");
    if (SettingActivity.playSounds(this)) {
    //如果时间等于零,切允许铃声提醒,则播放结束铃声
    playFinishedSound();
    }
    //if
    }//if
    }//else
    }//if



    if (remainingMeetingSeconds >0)
    remainingMeetingSeconds
    --;

    //使用Handler更新UI
    updateDisplayHandler.sendEmptyMessage(0);
    }
    在startTimer方法里生成了一个新的Timer 并通过内部类的方式 生成一个 TimerTask ,通过schedule()方法 指定updateTimerValuesTask 每个一秒运行。
    最后onResume中指定startTimer运行
    @Override
    protectedvoid onResume()
    {
    super.onResume();
    acquireWakeLock();
    startTimer();
    }
  • 相关阅读:
    vue中mixins的使用方法和注意点(详)
    vue中异步组件实现按需加载
    Vue动态组件
    Vue中slot的介绍与使用
    vue面试(二)
    菜单加载学习1
    NPOI,导出Execl,压缩文件zip,发送Email
    使用Microsoft EnterpriseLibrary(微软企业库)日志组件把系统日志写入数据库和xml文件
    框架公用方法
    Model Validation 和测试Post参数
  • 原文地址:https://www.cnblogs.com/keyindex/p/1824794.html
Copyright © 2011-2022 走看看