方法一
Timer与TimerTask(Java实现)
[java]
view plain
copy
print
?
1. public class timerTask extends Activity{
2.
3. private int recLen = 11;
4. private TextView txtView;
5. new Timer();
6.
7. public void onCreate(Bundle savedInstanceState){
8. super.onCreate(savedInstanceState);
9.
10. setContentView(R.layout.timertask);
11. txtView = (TextView)findViewById(R.id.txttime);
12.
13. 1000, 1000); // timeTask
14. }
15.
16. new TimerTask() {
17. @Override
18. public void run() {
19.
20. new Runnable() { // UI thread
21. @Override
22. public void run() {
23. recLen--;
24. ""+recLen);
25. if(recLen < 0){
26. timer.cancel();
27. txtView.setVisibility(View.GONE);
28. }
29. }
30. });
31. }
32. };
33. }
方法二
TimerTask与Handler(不用Timer的改进型)
[java]
view plain
copy
print
?
1. public class timerTask extends Activity{
2. private int recLen = 11;
3. private TextView txtView;
4. new Timer();
5.
6. public void onCreate(Bundle savedInstanceState){
7. super.onCreate(savedInstanceState);
8.
9. setContentView(R.layout.timertask);
10. txtView = (TextView)findViewById(R.id.txttime);
11.
12. 1000, 1000); // timeTask
13. }
14.
15. final Handler handler = new Handler(){
16. @Override
17. public void handleMessage(Message msg){
18. switch (msg.what) {
19. case 1:
20. ""+recLen);
21. if(recLen < 0){
22. timer.cancel();
23. txtView.setVisibility(View.GONE);
24. }
25. }
26. }
27. };
28.
29. new TimerTask() {
30. @Override
31. public void run() {
32. recLen--;
33. new Message();
34. 1;
35. handler.sendMessage(message);
36. }
37. };
38. }
方法三
Handler与Message(不用TimerTask)
[java]
view plain
copy
print
?
1. public class timerTask extends Activity{
2. private int recLen = 11;
3. private TextView txtView;
4.
5. public void onCreate(Bundle savedInstanceState) {
6. super.onCreate(savedInstanceState);
7.
8. setContentView(R.layout.timertask);
9. txtView = (TextView)findViewById(R.id.txttime);
10.
11. 1); // Message
12. 1000);
13. }
14.
15. final Handler handler = new Handler(){
16.
17. public void handleMessage(Message msg){ // handle message
18. switch (msg.what) {
19. case 1:
20. recLen--;
21. "" + recLen);
22.
23. if(recLen > 0){
24. 1);
25. 1000); // send message
26. else{
27. txtView.setVisibility(View.GONE);
28. }
29. }
30.
31. super.handleMessage(msg);
32. }
33. };
34. }
方法四
Handler与Thread(不占用UI线程)
[java]
view plain
copy
print
?
1. public class timerTask extends Activity{
2. private int recLen = 0;
3. private TextView txtView;
4.
5. public void onCreate(Bundle savedInstanceState){
6. super.onCreate(savedInstanceState);
7.
8. setContentView(R.layout.timertask);
9. txtView = (TextView)findViewById(R.id.txttime);
10.
11. new Thread(new MyThread()).start(); // start thread
12. }
13.
14. final Handler handler = new Handler(){ // handle
15. public void handleMessage(Message msg){
16. switch (msg.what) {
17. case 1:
18. recLen++;
19. "" + recLen);
20. }
21. super.handleMessage(msg);
22. }
23. };
24.
25. public class MyThread implements Runnable{ // thread
26. @Override
27. public void run(){
28. while(true){
29. try{
30. 1000); // sleep 1000ms
31. new Message();
32. 1;
33. handler.sendMessage(message);
34. catch (Exception e) {
35. }
36. }
37. }
38. }
39. }
方法五
Handler与Runnable(最简单型)
[java]
view plain
copy
print
?
1. public class timerTask extends Activity{
2. private int recLen = 0;
3. private TextView txtView;
4.
5. public void onCreate(Bundle savedInstanceState){
6. super.onCreate(savedInstanceState);
7.
8. setContentView(R.layout.timertask);
9. txtView = (TextView)findViewById(R.id.txttime);
10.
11. 1000);
12. }
13.
14. new Handler();
15. new Runnable() {
16. @Override
17. public void run() {
18. recLen++;
19. "" + recLen);
20. this, 1000);
21. }
22. };
23. }
计时与倒计时
方法1,方法2和方法3,都是倒计时
方法4,方法5,都是计时
计时和倒计时,都可使用上述方法实现(代码稍加改动)
UI线程比较
方法1,方法2和方法3,都是在UI线程实现的计时;
方法4和方法5,是另开Runnable线程实现计时
实现方式比较
方法1,采用的是Java实现,即Timer和TimerTask方式;
其它四种方法,都采用了Handler消息处理
推荐使用
如果对UI线程交互要求不很高,可以选择方法2和方法3
如果考虑到UI线程阻塞,严重影响到用户体验,推荐使用方法4,另起线程单独用于计时和其它的逻辑处理
方法5,综合了前几种方法的优点,是最简的