Android中Timer計時器詳解

來源:互聯網
上載者:User

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

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; 
        } 
    } 
} 

 

摘自 殤雲的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.