当前位置:操作系统 > 安卓/Android >>

Android中Timer计时器详解

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

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

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,