How to Implement timing and countdown in Android

Source: Internet
Author: User

Method 1

Timer and TimerTask (Java implementation)

Public class timerTask extends Activity {</p> <p> private int recLen = 11; <br/> private TextView txtView; <br/> Timer timer = new Timer (); </p> <p> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); </p> <p> setContentView (R. layout. timertask); <br/> txtView = (textview1_findviewbyid(r.id.txt time); </p> <p> timer. schedule (task, 1000,100 0); // timeTask <br/>}</p> <p> TimerTask task = new TimerTask () {<br/> @ Override <br/> public void run () {</p> <p> runOnUiThread (new Runnable () {// UI thread <br/> @ Override <br/> public void run () {<br/> recLen --; <br/> txtView. setText ("" + recLen); <br/> if (recLen <0) {<br/> timer. cancel (); <br/> txtView. setVisibility (View. GONE); <br/>}< br/>}); <br/>}< br/>}; <br/>}

Method 2

TimerTask and Handler (without the improved Timer)

Public class timerTask extends Activity {<br/> private int recLen = 11; <br/> private TextView txtView; <br/> Timer timer = new Timer (); </p> <p> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); </p> <p> setContentView (R. layout. timertask); <br/> txtView = (textview1_findviewbyid(r.id.txt time); </p> <p> timer. schedule (task, 1000,100 0); // timeTask <br/>}</p> <p> final Handler handler = new Handler () {<br/> @ Override <br/> public void handleMessage (Message msg) {<br/> switch (msg. what) {<br/> case 1: <br/> txtView. setText ("" + recLen); <br/> if (recLen <0) {<br/> timer. cancel (); <br/> txtView. setVisibility (View. GONE); <br/>}< br/>}; </p> <p> TimerTask task = new TimerTask () {<br/> @ Override <br/> public void run () {<br/> recLen --; <br/> Message message = new Message (); <br/> message. what = 1; <br/> handler. sendMessage (message); <br/>}< br/>}; <br/>}
Method 3

Handler and Message (without TimerTask)

Public class timerTask extends Activity {<br/> private int recLen = 11; <br/> private TextView txtView; </p> <p> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); </p> <p> setContentView (R. layout. timertask); <br/> txtView = (textview1_findviewbyid(r.id.txt time); </p> <p> Message message = handler. obtainMessage (1); // Message <br/> handler. sendMessageDelayed (message, 1000); <br/>}</p> <p> final Handler handler = new Handler () {</p> <p> public void handleMessage (Message msg) {// handle message <br/> switch (msg. what) {<br/> case 1: <br/> recLen --; <br/> txtView. setText ("" + recLen); </p> <p> if (recLen> 0) {<br/> Message message = handler. obtainMessage (1); <br/> handler. sendMessageDelayed (message, 1000); // send message <br/>}else {<br/> txtView. setVisibility (View. GONE); <br/>}</p> <p> super. handleMessage (msg); <br/>}< br/>}; <br/>}
Method 4

Handler and Thread (do not occupy the UI Thread)

Public class timerTask extends Activity {<br/> private int recLen = 0; <br/> private TextView txtView; </p> <p> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); </p> <p> setContentView (R. layout. timertask); <br/> txtView = (textview1_findviewbyid(r.id.txt time); </p> <p> new Thread (new MyThread ()). start (); // start thread <br/>}</p> <p> final Handler handler = new Handler () {// handle <br/> public void handleMessage (Message msg) {<br/> switch (msg. what) {<br/> case 1: <br/> recLen ++; <br/> txtView. setText ("" + recLen); <br/>}< br/> super. handleMessage (msg); <br/>}< br/> }; </p> <p> public class MyThread implements Runnable {// thread <br/> @ Override <br/> public void run () {<br/> while (true) {<br/> try {<br/> Thread. sleep (1000); // sleep 1000 ms <br/> Message message = new Message (); <br/> message. what = 1; <br/> handler. sendMessage (message); <br/>} catch (Exception e) {<br/>}< br/>}
Method 5

Handler and Runnable (simplest type)

Public class timerTask extends Activity {<br/> private int recLen = 0; <br/> private TextView txtView; </p> <p> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); </p> <p> setContentView (R. layout. timertask); <br/> txtView = (textview1_findviewbyid(r.id.txt time); </p> <p> handler. postDelayed (runnable, 1000); <br/>}</p> <p> Handler handler = new Handler (); <br/> Runnable runnable = new Runnable () {<br/> @ Override <br/> public void run () {<br/> recLen ++; <br/> txtView. setText ("" + recLen); <br/> handler. postDelayed (this, 1000); <br/>}< br/>}; <br/>}

Timing and countdown

Method 1, method 2, and method 3 are all countdown

Method 4 and Method 5 are all timing

Both timing and countdown can be implemented using the above method (slightly changed code)

UI thread comparison

Method 1, method 2, and method 3 are all timing implemented in the UI thread;

Method 4 and Method 5 are separate Runnable threads for timing.

Comparison of implementation methods

Method 1 adopts Java implementation, namely, Timer and TimerTask;

Handler message processing is used in the other four methods.

Recommended

If you do not have high requirements on UI thread interaction, you can select methods 2 and 3.

If the UI thread blocking seriously affects the user experience, Method 4 is recommended. Another thread is used separately for timing and other logic processing.

Method 5, combining the advantages of the previous methods, is the simplest

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.