Android中Timer计时器详解
2013-11-02 20:15
411 查看
直接上代码,解释看注释,一个火箭发射倒计时的例子 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计划的任务。 简单的一个例程:
|
相关文章推荐
- Android中Timer计时器详解
- Android中Timer计时器详解
- Android 动态注册监听网络变化实例详解
- 【Android应用开发详解】第01期:第三方授权认证(一)实现第三方授权登录、分享以及获取用户资料
- Android中Animation详解
- Android系统五大布局详解Layout
- Android SurfaceView 详解(二)
- 详解Android中AsyncTask的使用
- Android开发--Intent-filter属性详解
- android Fragments详解
- Android Intent详解
- android Spinner控件详解
- Android开发EditText属性详解
- Android 反编译apk 详解
- Android 图片缩放与旋转的实现详解
- Android Animations动画使用详解
- Android系统APN配置详解
- Android数据存储(二)----PreferenceFragment详解
- 【视图控件篇】自定义Android控件之IOS滑动开关模拟详解
- Android中的几种网络请求方式详解 .