Android Timer計時器詳解

直接上代碼,解釋看註釋,一個火箭發射倒計時的例子 html

main.xml java


[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" > android

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="開始倒計時" /> app

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

</LinearLayout>
TimerDemoActivity.java this


[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; 
        } 
    } 
線程

相關文章
相關標籤/搜索