android简易倒计时器 from manying in iteye


 

      这是一个android平台的简易倒计时器,可以实现设置时间的倒计时以及时间到后的提醒功能。

      android sdk提供的CountdownTimer 类已经实现的倒计时功能,但却没有暂停,继续等功能,在这个类的基础上可以实现一个自己的计时类。

      package com.util;

Java代码   收藏代码
  1. import android.os.Handler;  
  2. import android.os.Message;  
  3. //在android自带的CountdownTimer类基础上实现定时功能  
  4. public abstract class AdvancedCountdownTimer {  
  5.     private static final int MSG_RUN = 1;     
  6.   
  7.     private final long mCountdownInterval;// 定时间隔,以毫秒计  
  8.     private long mTotalTime;// 定时时间  
  9.     private long mRemainTime;// 剩余时间  
  10.   
  11.     // 构造函数  
  12.     public AdvancedCountdownTimer(long millisInFuture, long countDownInterval) {  
  13.         mTotalTime = millisInFuture;  
  14.         mCountdownInterval = countDownInterval;  
  15.         mRemainTime = millisInFuture;  
  16.     }  
  17.   
  18.     // 取消计时  
  19.     public final void cancel() {  
  20.         mHandler.removeMessages(MSG_RUN);  
  21.     }  
  22.   
  23.     // 重新开始计时  
  24.     public final void resume() {  
  25.         mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_RUN));  
  26.     }  
  27.   
  28.     // 暂停计时  
  29.     public final void pause() {  
  30.         mHandler.removeMessages(MSG_RUN);  
  31.     }  
  32.   
  33.     // 开始计时  
  34.     public synchronized final AdvancedCountdownTimer start() {  
  35.         if (mRemainTime <= 0) {// 计时结束后返回  
  36.             onFinish();  
  37.             return this;  
  38.         }  
  39.         // 设置计时间隔  
  40.         mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RUN),  
  41.                 mCountdownInterval);  
  42.         return this;  
  43.     }  
  44.   
  45.     public abstract void onTick(long millisUntilFinished, int percent);// 计时中  
  46.   
  47.     public abstract void onFinish();// 计时结束  
  48.   
  49.     // 通过handler更新android UI,显示定时时间  
  50.     private Handler mHandler = new Handler() {  
  51.   
  52.         @Override  
  53.         public void handleMessage(Message msg) {  
  54.   
  55.             synchronized (AdvancedCountdownTimer.this) {  
  56.                 if (msg.what == MSG_RUN) {  
  57.                     mRemainTime = mRemainTime - mCountdownInterval;  
  58.   
  59.                     if (mRemainTime <= 0) {  
  60.                         onFinish();  
  61.                     } else if (mRemainTime < mCountdownInterval) {  
  62.                         sendMessageDelayed(obtainMessage(MSG_RUN), mRemainTime);  
  63.                     } else {  
  64.   
  65.                         onTick(mRemainTime, new Long(100  
  66.                                 * (mTotalTime - mRemainTime) / mTotalTime)  
  67.                                 .intValue());  
  68.   
  69.                         sendMessageDelayed(obtainMessage(MSG_RUN),  
  70.                                 mCountdownInterval);  
  71.                     }  
  72.                 }   
  73.             }  
  74.         }  
  75.     };  
  76. }  

      这是一个抽象工具类,在主程序中编写一个类继承它就可以实现相应的计时功能。

 

Java代码   收藏代码
  1. package com.timer;  
  2.   
  3. import java.io.IOException;  
  4. import com.util.AdvancedCountdownTimer;  
  5. import android.app.Activity;  
  6. import android.app.AlertDialog;  
  7. import android.content.DialogInterface;  
  8. import android.media.MediaPlayer;  
  9. import android.media.MediaPlayer.OnCompletionListener;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.EditText;  
  14. import android.widget.TextView;  
  15. import android.widget.Button;  
  16. import android.widget.Toast;  
  17.   
  18. public class Timer extends Activity implements OnClickListener {  
  19.   
  20.     private TextView txt_time = null;//显示剩余时间  
  21.     private EditText edt_hour = null;//定时时间编辑框  
  22.     private EditText edt_minute = null;  
  23.     private EditText edt_second = null;  
  24.     private Button btn_start = null;//各个按钮  
  25.     private Button btn_pause = null;  
  26.     private Button btn_con = null;  
  27.     private Button btn_reset = null;  
  28.     private Button btn_exit = null;  
  29.   
  30.     private long hour = 0;//时间变量  
  31.     private long minute = 0;  
  32.     private long second = 0;  
  33.     private long time = 0;  
  34.   
  35.     private MyCount count = null;//定时类对象  
  36.   
  37.     /** Called when the activity is first created. */  
  38.     @Override  
  39.     public void onCreate(Bundle savedInstanceState) {  
  40.         super.onCreate(savedInstanceState);  
  41.         setContentView(R.layout.main);  
  42.         findViews();  
  43.         setListener();  
  44.         }  
  45.     //识别各个控件并设置按钮状态  
  46.     private void findViews() {  
  47.         txt_time = (TextView) findViewById(R.id.text);  
  48.         edt_hour = (EditText) findViewById(R.id.hour);  
  49.         edt_minute = (EditText) findViewById(R.id.minute);  
  50.         edt_second = (EditText) findViewById(R.id.second);  
  51.         btn_start = (Button) findViewById(R.id.start);  
  52.         btn_pause = (Button) findViewById(R.id.pause);  
  53.         btn_con = (Button) findViewById(R.id.con);  
  54.         btn_reset = (Button) findViewById(R.id.reset);  
  55.         btn_exit = (Button) findViewById(R.id.exit);  
  56.         btn_pause.setEnabled(false);  
  57.         btn_con.setEnabled(false);  
  58.   
  59.     }  
  60.     //绑定监听器  
  61.     private void setListener() {  
  62.         btn_start.setOnClickListener(this);  
  63.         btn_pause.setOnClickListener(this);  
  64.         btn_con.setOnClickListener(this);  
  65.         btn_reset.setOnClickListener(this);  
  66.         btn_exit.setOnClickListener(this);  
  67.   
  68.     }  
  69.    //获取设置的时间并判断输入是否为空  
  70.     private boolean setTime() {  
  71.         String strHour = edt_hour.getText().toString();//取控件的值  
  72.         String strMinute = edt_minute.getText().toString();  
  73.         String strSecond = edt_second.getText().toString();  
  74.         if (strHour.equals("") || strMinute.equals("") || strSecond.equals("")) {  
  75.             return false;  
  76.         } else {  
  77.             hour = Long.parseLong(strHour);  
  78.             minute = Long.parseLong(strMinute);  
  79.             second = Long.parseLong(strSecond);  
  80.             time = (hour * 3600 + minute * 60 + second) * 1000;  
  81.             count = new MyCount(time, 1000);  
  82.             return true;  
  83.         }  
  84.   
  85.     }  
  86.     //设置监听器  
  87.     @Override  
  88.     public void onClick(View v) {  
  89.         switch (v.getId()) {  
  90.         //点击开始计时按钮  
  91.         case R.id.start:  
  92.             if (setTime() == false) {  
  93.                 Toast.makeText(Timer.this"请输入完整定时时间", Toast.LENGTH_LONG)  
  94.                         .show();  
  95.             } else {  
  96.                 count.start();//开始计时  
  97.                 btn_start.setEnabled(false);  
  98.                 btn_pause.setEnabled(true);  
  99.             }  
  100.             break;  
  101.             //点击暂停按钮  
  102.         case R.id.pause:  
  103.             count.pause();//暂停  
  104.             btn_pause.setEnabled(false);  
  105.             btn_con.setEnabled(true);  
  106.             break;  
  107.             //点击继续按钮  
  108.         case R.id.con:  
  109.             count.resume();//继续  
  110.             btn_pause.setEnabled(true);  
  111.             btn_con.setEnabled(false);  
  112.             break;  
  113.             //点击重置按钮  
  114.         case R.id.reset:  
  115.             count.cancel();//取消及时,重置界面状态  
  116.             txt_time.setText("");  
  117.             edt_hour.setText("0");  
  118.             edt_hour.setText("0");  
  119.             edt_second.setText("30");  
  120.             btn_pause.setEnabled(false);  
  121.             btn_con.setEnabled(false);  
  122.             btn_start.setEnabled(true);           
  123.             break;  
  124.             //点击退出按钮  
  125.         case R.id.exit:  
  126.             android.os.Process.killProcess(android.os.Process.myPid());//退出  
  127.             break;  
  128.         default:  
  129.             break;  
  130.   
  131.         }  
  132.   
  133.     }  
  134.     //实现计时功能的类  
  135.     class MyCount extends AdvancedCountdownTimer {  
  136.   
  137.         public MyCount(long millisInFuture, long countDownInterval) {  
  138.             super(millisInFuture, countDownInterval);  
  139.         }  
  140.   
  141.         @Override  
  142.         public void onFinish() {  
  143.             //媒体对象  
  144.             final MediaPlayer media = MediaPlayer.create(Timer.this, R.raw.alarm);  
  145.             //对话框对象  
  146.             final AlertDialog mydialog = new AlertDialog.Builder(Timer.this).setMessage(  
  147.                     "时间到!").setPositiveButton("确定"new DialogInterface.OnClickListener(){  
  148.   
  149.                         @Override  
  150.                         public void onClick(DialogInterface dialog, int which) {  
  151.                             media.stop();  
  152.                             media.release();  
  153.                             btn_pause.setEnabled(false);  
  154.                             btn_con.setEnabled(false);  
  155.                             btn_start.setEnabled(true);   
  156.                         }  
  157.                           
  158.                     }).create();  
  159.             mydialog.show();//播放结束后显示对话框  
  160.             txt_time.setText("");  
  161.             try {  
  162.                 media.prepare();    //准备播放音乐              
  163.             } catch (IllegalStateException e) {  
  164.                 e.printStackTrace();  
  165.             } catch (IOException e) {  
  166.                 e.printStackTrace();  
  167.             }  
  168.             media.start();//播放音乐  
  169.             media.setOnCompletionListener(new OnCompletionListener() {//播放结束后会话框消失,重置按钮状态  
  170.                 @Override  
  171.                 public void onCompletion(MediaPlayer mp) {  
  172.                     mydialog.hide();  
  173.                     btn_pause.setEnabled(false);  
  174.                     btn_con.setEnabled(false);  
  175.                     btn_start.setEnabled(true);   
  176.   
  177.                 }  
  178.             });  
  179.   
  180.         }     
  181.         //更新剩余时间  
  182.         @Override  
  183.         public void onTick(long millisUntilFinished, int percent) {  
  184.             long myhour = (millisUntilFinished / 1000) / 3600;  
  185.             long myminute = ((millisUntilFinished / 1000) - myhour * 3600) / 60;  
  186.             long mysecond = millisUntilFinished / 1000 - myhour * 3600  
  187.                     - myminute * 60;  
  188.             txt_time.setText("剩余时间" + myhour + ":" + myminute + ":" + mysecond);                  
  189.   
  190.         }  
  191.   
  192.     }  
  193.   
  194. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值