在Android中使用Timer,并创建一个应用程序

都知道java中有一个Timer:java.util.Timer;

Android中也可使用。下面来看看源代码:

public class TimerActivity extends Activity {
	long k = 0;
	long wait = 0;
	long length = 0;
	boolean isstart = false;
	MediaPlayer mp;
	Timer timer;
    EditText txtWait;
    EditText txtLength;
    Button btnStart;
    Handler HANDLER = new Handler() {
    	@Override
    	public void handleMessage(Message msg) {
    		Bundle bundle=msg.getData();
    		switch(bundle.getInt("timer")) {
    		case 0x0000001:
    			Stop();
    			break;
    		}
    		super.handleMessage(msg);
    	}
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtWait = (EditText) findViewById(R.id.txtWait);
        wait = Integer.parseInt(txtWait.getText().toString());
        txtLength = (EditText) findViewById(R.id.txtLength);
        length = Integer.parseInt(txtLength.getText().toString());
        btnStart = (Button) findViewById(R.id.btnStart);
        mp = MediaPlayer.create(this, R.raw.alarm);
	timer = new Timer();
        btnStart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(!isstart) {
					Start();
				} else {
					isstart = false; k = 0;
					//省略
					Stop();
					timer.cancel();
				}		
			}
		});
    }
    void Start(){
		timer.purge();
		timer = new Timer();
		timer.schedule(new MyTask(), wait*1000, 1000);
		isstart = true;
		//省略
    }
    void Stop(){
		//省略
    }
    class MyTask extends TimerTask {
		@Override
		public void run() {
			if(k==wait*1000) {
				//省略
			}
			if(k==(wait+length)*1000) {
				isstart = false; k = 0;
				timer.cancel();
				Message msg = HANDLER.obtainMessage();
				Bundle bundle = new Bundle();
				bundle.putInt("timer", 0x0000001);
				msg.setData(bundle);
				HANDLER.sendMessage(msg);
				timer.cancel();
				//省略
			}
			k+=1000;
		}
    }
}


main.xml就不解释了。

首先在‘省略’里面写你自己的一些代码,

我在这里使用了Handler,原因待会揭晓。

Handler中处理了Message,然后处理了Message,注意,是在main线程中处理的。

onCreate方法里,我们为btnStart添加了onClickListener,处理Timer,点击以后,Timer开始执行,再点一次,Timer停止了。

然后在Start方法里,为timer设置了TimerTask:timer.schedule(new MyTask(), wait*1000, 1000); wait*1000是Timer等待时间,以毫秒为单位, 第二个1000是指Timer的执行周期,为1秒。

MyTask 中有一个run方法,是上面的每1秒执行一次,我们的程序是计时工具,在if(k==(wait+length)*1000)中,我们处理了timer.cencel()的一些事情,我们发送了一个Message给Activity线程中的Handler,是为了让Activity处理Timer结束的一些消息,比如修改Button的Text,只能在Activity中处理,否则会报错:跨线程的错误,由此我们可以得知,TiemrTask是在新的线程中执行的。

其实,我们可以单独使用Handler进行计时的操作:

Runnable r = new Runnable(){
 public void run() {
  //刚才在TimerTask中的内容
  HANDLER.postDelayed(r, 1000);
 }
};
HANDLER.postDelayed(r, 1000);//这与刚才的timer.schedule(new TimerTask(), wait, period)相似,wait我们可以在HANDLER第一次postDelayed中将Delayed设置为wait,再在Runnable中的HANDLER.postDelayed中设置Delayed为length.(wait与length的含义参考TimerActivity的源代码)这样,我们也可以实现Timer,并且在同一线程中。要是想在不同线程中,可以使用Timer。


要cencel这个HANDLER消息队列,我们在Runnable里的run方法中的HANDLER.postDelayed()加一个if就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值