In promotions on shopping websites, there is usually a countdown to limit the shopping time or discount time. How can this be achieved?
A similar problem occurs in an android client project. timer and timertask are used at the beginning. Although this method is common, whether there is a better solution in Android is considered, as a result, we have found the following five implementation solutions. In addition, we will introduce how to use countdowntimer for timing.
Effect:
Method 1
Timer and timertask (Java implementation)
public class timerTask extends Activity{ private int recLen = 11; private TextView txtView; Timer timer = new Timer(); public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); timer.schedule(task, 1000, 1000); // timeTask } TimerTask task = new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { // UI thread @Override public void run() { recLen--; txtView.setText(""+recLen); if(recLen < 0){ timer.cancel(); txtView.setVisibility(View.GONE); } } }); } }; }
Method 2
Timertask and handler (without the improved timer)
public class timerTask extends Activity{ private int recLen = 11; private TextView txtView; Timer timer = new Timer(); public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); timer.schedule(task, 1000, 1000); // timeTask } final Handler handler = new Handler(){ @Override public void handleMessage(Message msg){ switch (msg.what) { case 1: txtView.setText(""+recLen); if(recLen < 0){ timer.cancel(); txtView.setVisibility(View.GONE); } } } }; TimerTask task = new TimerTask() { @Override public void run() { recLen--; Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; }
Method 3
Handler and message (without timertask) public class timertask extends activity {private int reclen = 11; private textview txtview; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. timertask); txtview = (textview1_findviewbyid(r.id.txt time); message = handler. obtainmessage (1); // message handler. sendmessagedelayed (message, 1000);} final handler = new handler () {public void handlemessage (Message MSG) {// handle message switch (MSG. what) {Case 1: reclen --; txtview. settext ("" + reclen); If (reclen> 0) {message = handler. obtainmessage (1); handler. sendmessagedelayed (message, 1000); // send message} else {txtview. setvisibility (view. gone) ;}} super. handlemessage (MSG );}};}
Method 4
Handler and thread (do not occupy the UI thread)
public class timerTask extends Activity{ private int recLen = 0; private TextView txtView; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.timertask); txtView = (TextView)findViewById(R.id.txttime); new Thread(new MyThread()).start(); // start thread } final Handler handler = new Handler(){ // handle public void handleMessage(Message msg){ switch (msg.what) { case 1: recLen++; txtView.setText("" + recLen); } super.handleMessage(msg); } }; public class MyThread implements Runnable{ // thread @Override public void run(){ while(true){ try{ Thread.sleep(1000); // sleep 1000ms Message message = new Message(); message.what = 1; handler.sendMessage(message); }catch (Exception e) { }
Method 5
Handler and runnable (simplest type) public class timertask extends activity {private int reclen = 0; private textview txtview; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. timertask); txtview = (textview1_findviewbyid(r.id.txt time); runnable. run ();} handler = new handler (); runnable = new runnable () {@ override public void run () {reclen ++; txtview. settext ("" + reclen); handler. postdelayed (this, 1000 );}};}
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