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.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.");
}
}
- Android中Timer计时器详解
- Android中Timer计时器详解
- Android中Timer计时器详解
- Android简单计时器详解(Timer)
- Android开发中如何实现Timer计时器
- Android中timer(计时器)的使用
- Android 计时器Timer用法
- Android 计时器Timer用法
- Android 计时器Timer用法
- android 计时器 Timer
- Android 计时器Timer用法
- Android 计时器Timer用法
- Android 计时器Timer用法
- android 计时器Timer
- Android 计时器Timer用法
- android--计时器(Timer)
- Android 计时器Timer用法
- Android 计时器Timer用法
- 一。进行内核修改、配置、编译的基础准备工作。
- Gem/Bundle/Rvm
- 利用STK进行卫星星际链路分析
- how vc convert LR LF to LR
- 在ubuntu13.04中使用vsftpd搭建FTP服务器
- Android中Timer计时器详解
- IO流总结
- C++技巧 :C#和C++结构体Socket通信
- eclipse工具使用总结(java,android配置等)
- 如何查看苹果计算机开器了哪些系统
- AndroidIntent应用,打开网页,安装程序,打电话,打开地图等功能 .
- 深入java虚拟机(二)——类的生命周期(上)类的加载和连接
- C++多线程SOCKET通信研究日志
- WD MyBook Live安装SVN