这是一个android平台的简易倒计时器,可以实现设置时间的倒计时以及时间到后的提醒功能。
android sdk提供的CountdownTimer 类已经实现的倒计时功能,但却没有暂停,继续等功能,在这个类的基础上可以实现一个自己的计时类。
package com.util;
- import android.os.Handler;
- import android.os.Message;
- //在android自带的CountdownTimer类基础上实现定时功能
- public abstract class AdvancedCountdownTimer {
- private static final int MSG_RUN = 1;
- private final long mCountdownInterval;// 定时间隔,以毫秒计
- private long mTotalTime;// 定时时间
- private long mRemainTime;// 剩余时间
- // 构造函数
- public AdvancedCountdownTimer(long millisInFuture, long countDownInterval) {
- mTotalTime = millisInFuture;
- mCountdownInterval = countDownInterval;
- mRemainTime = millisInFuture;
- }
- // 取消计时
- public final void cancel() {
- mHandler.removeMessages(MSG_RUN);
- }
- // 重新开始计时
- public final void resume() {
- mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_RUN));
- }
- // 暂停计时
- public final void pause() {
- mHandler.removeMessages(MSG_RUN);
- }
- // 开始计时
- public synchronized final AdvancedCountdownTimer start() {
- if (mRemainTime <= 0) {// 计时结束后返回
- onFinish();
- return this;
- }
- // 设置计时间隔
- mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RUN),
- mCountdownInterval);
- return this;
- }
- public abstract void onTick(long millisUntilFinished, int percent);// 计时中
- public abstract void onFinish();// 计时结束
- // 通过handler更新android UI,显示定时时间
- private Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- synchronized (AdvancedCountdownTimer.this) {
- if (msg.what == MSG_RUN) {
- mRemainTime = mRemainTime - mCountdownInterval;
- if (mRemainTime <= 0) {
- onFinish();
- } else if (mRemainTime < mCountdownInterval) {
- sendMessageDelayed(obtainMessage(MSG_RUN), mRemainTime);
- } else {
- onTick(mRemainTime, new Long(100
- * (mTotalTime - mRemainTime) / mTotalTime)
- .intValue());
- sendMessageDelayed(obtainMessage(MSG_RUN),
- mCountdownInterval);
- }
- }
- }
- }
- };
- }
这是一个抽象工具类,在主程序中编写一个类继承它就可以实现相应的计时功能。
- package com.timer;
- import java.io.IOException;
- import com.util.AdvancedCountdownTimer;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Button;
- import android.widget.Toast;
- public class Timer extends Activity implements OnClickListener {
- private TextView txt_time = null;//显示剩余时间
- private EditText edt_hour = null;//定时时间编辑框
- private EditText edt_minute = null;
- private EditText edt_second = null;
- private Button btn_start = null;//各个按钮
- private Button btn_pause = null;
- private Button btn_con = null;
- private Button btn_reset = null;
- private Button btn_exit = null;
- private long hour = 0;//时间变量
- private long minute = 0;
- private long second = 0;
- private long time = 0;
- private MyCount count = null;//定时类对象
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViews();
- setListener();
- }
- //识别各个控件并设置按钮状态
- private void findViews() {
- txt_time = (TextView) findViewById(R.id.text);
- edt_hour = (EditText) findViewById(R.id.hour);
- edt_minute = (EditText) findViewById(R.id.minute);
- edt_second = (EditText) findViewById(R.id.second);
- btn_start = (Button) findViewById(R.id.start);
- btn_pause = (Button) findViewById(R.id.pause);
- btn_con = (Button) findViewById(R.id.con);
- btn_reset = (Button) findViewById(R.id.reset);
- btn_exit = (Button) findViewById(R.id.exit);
- btn_pause.setEnabled(false);
- btn_con.setEnabled(false);
- }
- //绑定监听器
- private void setListener() {
- btn_start.setOnClickListener(this);
- btn_pause.setOnClickListener(this);
- btn_con.setOnClickListener(this);
- btn_reset.setOnClickListener(this);
- btn_exit.setOnClickListener(this);
- }
- //获取设置的时间并判断输入是否为空
- private boolean setTime() {
- String strHour = edt_hour.getText().toString();//取控件的值
- String strMinute = edt_minute.getText().toString();
- String strSecond = edt_second.getText().toString();
- if (strHour.equals("") || strMinute.equals("") || strSecond.equals("")) {
- return false;
- } else {
- hour = Long.parseLong(strHour);
- minute = Long.parseLong(strMinute);
- second = Long.parseLong(strSecond);
- time = (hour * 3600 + minute * 60 + second) * 1000;
- count = new MyCount(time, 1000);
- return true;
- }
- }
- //设置监听器
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- //点击开始计时按钮
- case R.id.start:
- if (setTime() == false) {
- Toast.makeText(Timer.this, "请输入完整定时时间", Toast.LENGTH_LONG)
- .show();
- } else {
- count.start();//开始计时
- btn_start.setEnabled(false);
- btn_pause.setEnabled(true);
- }
- break;
- //点击暂停按钮
- case R.id.pause:
- count.pause();//暂停
- btn_pause.setEnabled(false);
- btn_con.setEnabled(true);
- break;
- //点击继续按钮
- case R.id.con:
- count.resume();//继续
- btn_pause.setEnabled(true);
- btn_con.setEnabled(false);
- break;
- //点击重置按钮
- case R.id.reset:
- count.cancel();//取消及时,重置界面状态
- txt_time.setText("");
- edt_hour.setText("0");
- edt_hour.setText("0");
- edt_second.setText("30");
- btn_pause.setEnabled(false);
- btn_con.setEnabled(false);
- btn_start.setEnabled(true);
- break;
- //点击退出按钮
- case R.id.exit:
- android.os.Process.killProcess(android.os.Process.myPid());//退出
- break;
- default:
- break;
- }
- }
- //实现计时功能的类
- class MyCount extends AdvancedCountdownTimer {
- public MyCount(long millisInFuture, long countDownInterval) {
- super(millisInFuture, countDownInterval);
- }
- @Override
- public void onFinish() {
- //媒体对象
- final MediaPlayer media = MediaPlayer.create(Timer.this, R.raw.alarm);
- //对话框对象
- final AlertDialog mydialog = new AlertDialog.Builder(Timer.this).setMessage(
- "时间到!").setPositiveButton("确定", new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- media.stop();
- media.release();
- btn_pause.setEnabled(false);
- btn_con.setEnabled(false);
- btn_start.setEnabled(true);
- }
- }).create();
- mydialog.show();//播放结束后显示对话框
- txt_time.setText("");
- try {
- media.prepare(); //准备播放音乐
- } catch (IllegalStateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- media.start();//播放音乐
- media.setOnCompletionListener(new OnCompletionListener() {//播放结束后会话框消失,重置按钮状态
- @Override
- public void onCompletion(MediaPlayer mp) {
- mydialog.hide();
- btn_pause.setEnabled(false);
- btn_con.setEnabled(false);
- btn_start.setEnabled(true);
- }
- });
- }
- //更新剩余时间
- @Override
- public void onTick(long millisUntilFinished, int percent) {
- long myhour = (millisUntilFinished / 1000) / 3600;
- long myminute = ((millisUntilFinished / 1000) - myhour * 3600) / 60;
- long mysecond = millisUntilFinished / 1000 - myhour * 3600
- - myminute * 60;
- txt_time.setText("剩余时间" + myhour + ":" + myminute + ":" + mysecond);
- }
- }
- }