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

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. Android Sqlite 数据库多线程操作
  2. [Android] 图片JNI(C++\Java)高斯模糊 多线程
  3. SQLite多线程操作数据库
  4. Android “2019-09-11T00:00:00+09:00“格式时间转换
  5. android时间比较
  6. Android(java)时间转换星期 昨天 今天 几分钟前工具
  7. Android 隔一段时间重复执行某代码

随机推荐

  1. Android开发学习:ImageView的scaletype属
  2. android 高德amap开发一(地图创建)
  3. Android(安卓)中文 API (26) —— SeekBar
  4. android中Bitmap的放大和缩小的方法
  5. 如何使用arm-eabi-gdb调试android c/c++
  6. android fragment(android.support.v4.app
  7. 【Android(安卓)okhttp源码解析 五】拦截
  8. android linux 最全的基础知识总结
  9. Android(安卓)- 开发实例(14):透明SystemB
  10. Android事件分发和View绘制流程分析(三)