Android中Timer计时器详解

来源:互联网 发布:深圳远望数码 淘宝 编辑:程序博客网 时间:2024/05/01 14:51

直接上代码,解释看注释,一个火箭发射倒计时的例子

main.xml


[html] <?xml version="1.0" encoding="utf-8"?> 
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<Button 
android:id="@+id/button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="开始倒计时" /> 

<TextView 
android:id="@+id/textView" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" /> 

< /LinearLayout> 
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始倒计时" />

<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>
TimerDemoActivity.java


[java] package com.tianjf; 

import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class TimerDemoActivity extends Activity implements OnClickListener { 

private Button button; 
private TextView textView; 
private Timer timer; 

// 定义Handler 
Handler handler = new Handler() { 

@Override 
public void handleMessage(Message msg) { 
super.handleMessage(msg); 

Log.d("debug", "handleMessage方法所在的线程:" 
+ Thread.currentThread().getName()); 

// Handler处理消息 
if (msg.what > 0) { 
textView.setText(msg.what + ""); 
} else { 
textView.setText("点火!"); 
// 结束Timer计时器 
timer.cancel(); 


}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

button = (Button) findViewById(R.id.button); 
textView = (TextView) findViewById(R.id.textView); 

Log.d("debug", "onCreate方法所在的线程:" 
+ Thread.currentThread().getName()); 
button.setOnClickListener(this); 


@Override 
public void onClick(View v) { 
switch (v.getId()) { 
case R.id.button: 
// 按钮按下时创建一个Timer定时器 
timer = new Timer(); 
// 创建一个TimerTask 
// TimerTask是个抽象类,实现了Runnable接口,所以TimerTask就是一个子线程 
TimerTask timerTask = new TimerTask() { 
// 倒数10秒 
int i = 10; 

@Override 
public void run() { 
Log.d("debug", "run方法所在的线程:" 
+ Thread.currentThread().getName()); 
// 定义一个消息传过去 
Message msg = new Message(); 
msg.what = i--; 
handler.sendMessage(msg); 

}; 
// 定义计划任务,根据参数的不同可以完成以下种类的工作: 
// 1.schedule(TimerTask task, Date when) ー> 在固定时间执行某任务 
// 2.schedule(TimerTask task, Date when, long period) ー> 在固定时间开始重复执行某任务,重复时间间隔可控 
// 3.schedule(TimerTask task, long delay) ー> 在延迟多久后执行某任务 
// 4.schedule(TimerTask task, long delay, long period) ー> 在延迟多久后重复执行某任务,重复时间间隔可控 
timer.schedule(timerTask, 3000, 1000);// 3秒后开始倒计时,倒计时间隔为1秒 
break; 

default: 
break; 


}

 

************************

1.概览

Timer是一种定时器工具,用来在一个后台线程计划执行指定任务。它可以计划执行一个任务一次或反复多次。
TimerTask一个抽象类,它的子类代表一个可以被Timer计划的任务。

简单的一个例程:

import java.util.Timer;
import java.util.TimerTask;

/**
* Simple demo that uses java.util.Timer to schedule a task to execute
* once 5 seconds have passed.
*/

public class Reminder {
Timer timer;

public Reminder(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);

}

class RemindTask extends TimerTask {
public void run() {

System.out.println("Time's up!");
timer.cancel(); //Terminate the timer thread
}
}

public static void main(String args[]) {
System.out.println("About to schedule task.");
new Reminder(5);
System.out.println("Task scheduled.");
}
}

原创粉丝点击