Android簡單計時器詳解(Timer)
阿新 • • 發佈:2019-02-15
Android計時器詳解(Timer)
在開發中我們有時會有這樣的需求,即在固定的每隔一段時間執行某一個任務。比如UI上的控制元件需要隨著時間改變,我們可以使用Java為我們提供的計時器的工具類,即Timer和TimerTask。
API
Timer是一個普通的類,其中有幾個重要的方法;而TimerTask則是一個抽象類,其中有一個抽象方法run(),類似執行緒中的run()方法,我們使用Timer建立一個他的物件,然後使用這物件的schedule方法來完成這種間隔的操作。
Android考慮到執行緒安全問題,不允許線上程中執行UI執行緒。
android.os.Handler,這個可以實現各處執行緒間的訊息傳遞。
schedule方法有三個引數 第一個引數就是TimerTask型別的物件,我們實現TimerTask的run()方法就是要週期執行的一個任務; 第二個引數有兩種型別,第一種是long型別,表示多長時間後開始執行,另一種是Date型別,表示從那個時間後開始執行; 第三個引數就是執行的週期,為long型別。 schedule方法還有一種兩個引數的執行過載,第一個引數仍然是TimerTask,第二個表示為long的形式表示多長時間後執行一次,為Date就表示某個時間後執行一次。? Timer就是一個執行緒,使用schedule方法完成對TimerTask的排程,多個TimerTask可以共用一個Timer,也就是說Timer物件呼叫一次schedule方法就是建立了一個執行緒,並且呼叫一次schedule 後TimerTask是無限制的迴圈下去的,使用Timer的cancel()停止操作。當然同一個Timer執行一次cancel()方法後,所有Timer執行緒都被終止。
用法
java.util.Timer timer = new java.util.Timer(true); TimerTask task = new TimerTask() { public void run() { //每次需要執行的程式碼放到這裡面。 } }; //以下是幾種排程task的方法: //time為Date型別:在指定時間執行一次。 timer.schedule(task, time); //firstTime為Date型別,period為long,表示從firstTime時刻開始,每隔period毫秒執行一次。 timer.schedule(task, firstTime, period); //delay 為long型別:從現在起過delay毫秒執行一次。 timer.schedule(task, delay); //delay為long,period為long:從現在起過delay毫秒以後,每隔period毫秒執行一次。 timer.schedule(task, delay, period);
演示圖
程式碼
佈局程式碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<EditText
android:id="@+id/inputtime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/gettime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取倒計時時間" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/starttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開始計時" />
<Button
android:id="@+id/stoptime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止計時" />
</LinearLayout>
邏輯實現程式碼
package com.example.tmier;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private EditText inputet;//輸入時間
private Button getTime;//得到輸入的時間
private Button startTime;//點選開始自動減少時間的按鈕
private Button stopTime;//點選開始停止自動減少時間的按鈕
private TextView time;//當前時間
private int i = 0;
private Timer timer = null;//計時器
private TimerTask timerTask = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initviews();//初始化檢視
setOnclick();//設定檢視的點選監聽
}
private void setOnclick() {
getTime.setOnClickListener(this);
startTime.setOnClickListener(this);
stopTime.setOnClickListener(this);
}
private void initviews() {
inputet = (EditText) findViewById(R.id.inputtime);
getTime = (Button) findViewById(R.id.gettime);
startTime = (Button) findViewById(R.id.starttime);
stopTime = (Button) findViewById(R.id.stoptime);
time = (TextView) findViewById(R.id.time);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.gettime:
time.setText(inputet.getText().toString());//當前時間
i = Integer.parseInt(inputet.getText().toString());//當前輸入時間
break;
case R.id.starttime://開始自動減時
startTime();
break;
case R.id.stoptime://停止自動減時
stopTime();
break;
}
}
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
time.setText(msg.arg1+"");
startTime();
};
};
/**
* 開始自動減時
*/
private void startTime() {
if(timer==null){
timer = new Timer();
}
timerTask = new TimerTask() {
@Override
public void run() {
i--;//自動減1
Message message = Message.obtain();
message.arg1=i;
mHandler.sendMessage(message);//傳送訊息
}
};
timer.schedule(timerTask, 1000);//1000ms執行一次
}
/**
* 停止自動減時
*/
private void stopTime() {
if(timer!=null)
timer.cancel();
}
}