Android Timer 更好方法
阿新 • • 發佈:2019-01-31
Android 的Timer 一般如下面所寫
Java程式碼
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
Java程式碼
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Log.e("Timer", "Timer");
update();
break;
}
super.handleMessage(msg);
}
};
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Log.e("Timer", "Timer");
update();
break;
}
super.handleMessage(msg);
}
};
Java程式碼
protected void onDestroy() {
if (timer != null) {
timer.cancel();
timer = null;
}
super.onDestroy();
}
protected void onDestroy() {
if (timer != null) {
timer.cancel();
timer = null;
}
super.onDestroy();
}
最重要一點是timer.schedule(task, 1000, 5000);如果設定為timer.schedule(task, 5000)是不會工作的。
Java程式碼
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
timer.schedule(task, 1000, 5000);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
timer.schedule(task, 1000, 5000);
}
但更好的方法是通過下面的程式碼
Java程式碼
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {
update();
handler.postDelayed(this, 1000);
}
};
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {
update();
handler.postDelayed(this, 1000);
}
};
開始Timer
Java程式碼
handler.postDelayed(runnable, 1000);
handler.postDelayed(runnable, 1000);
停止Timer
Java程式碼
handler.removeCallbacks(runnable);
Java程式碼
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
Java程式碼
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Log.e("Timer", "Timer");
update();
break;
}
super.handleMessage(msg);
}
};
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Log.e("Timer", "Timer");
update();
break;
}
super.handleMessage(msg);
}
};
Java程式碼
protected void onDestroy() {
if (timer != null) {
timer.cancel();
timer = null;
}
super.onDestroy();
}
protected void onDestroy() {
if (timer != null) {
timer.cancel();
timer = null;
}
super.onDestroy();
}
最重要一點是timer.schedule(task, 1000, 5000);如果設定為timer.schedule(task, 5000)是不會工作的。
Java程式碼
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
timer.schedule(task, 1000, 5000);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
timer.schedule(task, 1000, 5000);
}
但更好的方法是通過下面的程式碼
Java程式碼
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {
update();
handler.postDelayed(this, 1000);
}
};
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {
update();
handler.postDelayed(this, 1000);
}
};
開始Timer
Java程式碼
handler.postDelayed(runnable, 1000);
handler.postDelayed(runnable, 1000);
停止Timer
Java程式碼
handler.removeCallbacks(runnable);