Detailed explanation and implementation methods of three types of timers in Android

Detailed explanation and implementation methods of three types of timers in Android

[[171357]]

This article mainly introduces the knowledge of Android timer. Here are three methods to implement the timer. Friends in need can refer to it.

Method 1: Handler+Thread

  1. package com.xunfang.handerDemo;
  2.    
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.widget.TextView;
  8.    
  9. /**
  10. * handler timer
  11. *
  12. * @author Smalt
  13. *
  14. */
  15. public class HanderDemoActivity extends Activity {
  16. TextView tvShow;
  17. private int i = 0 ;
  18.    
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. tvShow = (TextView) findViewById(R.id.tv_show);
  24. new Thread(new ThreadShow()).start();
  25. }
  26.    
  27. // The handler class receives data
  28. Handler handler = new Handler() {
  29. public void handleMessage(Message msg) {
  30. if ( msg.what == 1 ) {
  31. tvShow.setText(Integer.toString(i++));
  32. System.out.println("receive....");
  33. }
  34. };
  35. };
  36.    
  37. //Thread class
  38. class ThreadShow implements Runnable {
  39.    
  40. @Override
  41. public void run() {
  42. // TODO Auto-generated method stub
  43. while (true) {
  44. try {
  45. Thread.sleep(1000);
  46. Message msg = new Message();
  47. msg.what = 1 ;
  48. handler.sendMessage(msg);
  49. System.out.println("send...");
  50. } catch (Exception e) {
  51. // TODO Auto-generated catch block
  52. e.printStackTrace();
  53. System.out.println("thread error...");
  54. }
  55. }
  56. }
  57. }
  58. }

Method 2: postDelyed of the Handler class

  1. package com.xunfang.handerDemo;
  2.    
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.widget.TextView;
  7.    
  8. /**
  9. * The handler timer is implemented using postDelyed
  10. *
  11. * @author Smalt
  12. *
  13. */
  14. public class HanderDemoActivity extends Activity {
  15. TextView tvShow;
  16. private int i = 0 ;
  17. private int TIME = 1000 ;
  18.    
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. tvShow = (TextView) findViewById(R.id.tv_show);
  24. handler.postDelayed(runnable, TIME); //Execute every 1s
  25. }
  26.    
  27. Handler handler = new Handler();
  28. Runnable runnable = new Runnable() {
  29.    
  30. @Override
  31. public void run() {
  32. // The handler comes with its own method to implement the timer
  33. try {
  34. handler.postDelayed(this, TIME);
  35. tvShow.setText(Integer.toString(i++));
  36. System.out.println("do...");
  37. } catch (Exception e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. System.out.println("exception...");
  41. }
  42. }
  43. };
  44.    
  45. }

Method 3: Handler+Timer+TimerTask

  1. package com.xunfang.handerDemo;
  2.    
  3. import java.util.Timer;
  4. import java.util.TimerTask;
  5.    
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.os.Handler;
  9. import android.os.Message;
  10. import android.widget.TextView;
  11.    
  12. /**
  13. * Timer implementation: Handler+Timer+TimerTask
  14. *
  15. * @author Smalt
  16. *
  17. */
  18. public class HanderDemoActivity extends Activity {
  19. TextView tvShow;
  20. private int i = 0 ;
  21. private int TIME = 1000 ;
  22.    
  23. @Override
  24. public void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.main);
  27. tvShow = (TextView) findViewById(R.id.tv_show);
  28. timer.schedule(task, 1000, 1000); // Execute task after 1s, and execute again after 1s
  29. }
  30.    
  31. Handler handler = new Handler() {
  32. public void handleMessage(Message msg) {
  33. if ( msg.what == 1 ) {
  34. tvShow.setText(Integer.toString(i++));
  35. }
  36. super.handleMessage(msg);
  37. };
  38. };
  39. Timer timer = new Timer();
  40. TimerTask task = new TimerTask() {
  41.    
  42. @Override
  43. public void run() {
  44. // What needs to be done: send a message
  45. Message message = new Message();
  46. message.what = 1 ;
  47. handler.sendMessage(message);
  48. }
  49. };
  50. }

The above is the summary of the Android timer information. I will continue to add relevant knowledge in the future. Thank you for your support!

<<:  Tmall client's security model is the cornerstone of user experience

>>:  Summary of Android memory leaks (with memory detection tools)

Recommend

Analysis of the user system of Momo live broadcast product

There is a mind map at the end of the article. By...

Today is the beginning of winter丨Autumn is over and winter is coming

Today 18:45 We usher in the first solar term of w...