實現定時器有很多種方式,在這裡我簡單的介紹幾種方式
(1)使用Handler + Runnable的方式
[java]
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
//你要做的事
//......
System.out.println(Thread.currentThread().getName());
handler.postDelayed(runnable, 1000);
}
};
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
//你要做的事
//......
System.out.println(Thread.currentThread().getName());
handler.postDelayed(runnable, 1000);
}
};
然後調用handler.post(runnable);就能啟動定時器,這裡是每隔1s列印線程名字,從列印中我們可以知道,他並沒有另開線程,而是運行在UI線程當中,當你要取消定時器的時候,只需要調用handler.removeCallbacks(runnable)就可以了。
上面中有一個問題,有時候你會發現removeCallbacks有時候會失效,不能從訊息佇列中移除,看下面的demo
圖:兩個按鈕,一個將Runnable加到訊息佇列中,一個將Runnable從訊息佇列中移除。該Runnable每1秒鐘列印一次日誌。
[java]
<SPAN style="FONT-FAMILY: Courier New">package com.example.demoactivity;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TimerActivity extends Activity{
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("update...");
handler.postDelayed(runnable, 1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer);
Button mButtonStart = (Button) findViewById(R.id.button1);
Button mButtonStop = (Button) findViewById(R.id.button2);
mButtonStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handler.post(runnable);
}
});
mButtonStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handler.removeCallbacks(runnable);
}
});
}
}</SPAN><SPAN style="FONT-FAMILY: Georgia, 'Times new roman', Times, san-serif">
</SPAN>
package com.example.demoactivity;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TimerActivity extends Activity{
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("update...");
handler.postDelayed(runnable, 1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer);
Button mButtonStart = (Button) findViewById(R.id.button1);
Button mButtonStop = (Button) findViewById(R.id.button2);
mButtonStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handler.post(runnable);
}
});
mButtonStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handler.removeCallbacks(runnable);
}
});
}
}
結果:
(1)start –> 輸出 –> stop–> 停止輸出
(2)start –> 輸出 –> Background –> Front –> stop->繼續輸出
當Activity進入後台運行後再轉入前台運行,removeCallbacks無法將updateThread從message queue中移除。
這是為什麼呢?
在Activity由前台轉後台過程中,線程是一直在啟動並執行,但是當Activity轉入前台時會重新定義Runnable runnable;也就是說此時從message queue移除的runnable與原先加入message queue中的runnable並非是同一個對象。如果把runnable定義為靜態則removeCallbacks不會失效,對於靜態變數在記憶體中只有一個拷貝(節省記憶體),JVM只為靜態分配一次記憶體,在載入類的過程中完成靜態變數的記憶體配置,我們做如下修改就能解決上面的這個問題
[java]
static Handler handler = new Handler();
static Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("update...");
handler.postDelayed(runnable, 1000);
}
};
static Handler handler = new Handler();
static Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("update...");
handler.postDelayed(runnable, 1000);
}
};(2)使用Timer的方式
[java]
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("update....");
}
}, 0, 1000);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("update....");
}
}, 0, 1000);上面的每一秒列印語句,run方法是運行在子線程,不能直接在裡面更新UI操作,這裡需要注意下,取消的話調用timer.cancel()就能移除任務了
(3)採用Handle與線程的sleep(long )方法
1.定義一個Handler類,用於處理接受到的Message
[java]
Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println("update...");
}
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println("update...");
}
}2.建立一個實現Runnable介面的線程類,用一個boolean 來控制線程開始和結束 boolean isLive = true如下:
[java]
public class MyThread implements Runnable {
@Override
public void run() {
while (isLive) {
try {
Thread.sleep(1000);// 線程暫停1秒,單位毫秒
Message message = new Message();
message.what = 1;
handler.sendMessage(message);// 發送訊息
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class MyThread implements Runnable {
@Override
public void run() {
while (isLive) {
try {
Thread.sleep(1000);// 線程暫停1秒,單位毫秒
Message message = new Message();
message.what = 1;
handler.sendMessage(message);// 發送訊息
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3.在需要啟動線程的地方加入下面語句
[java]
new Thread(new MyThread()).start();
new Thread(new MyThread()).start();4.取消的話將isLive設定為false就行了
今天主要介紹這三種方法,寫的不好的地方希望大家指出,謝謝!