This article summarizes the common methods of timing and countdown of Android. Share to everyone for your reference, specific as follows:
Method One
Timer and TimerTask (Java implementation)
public class TimerTask extends activity{
private int reclen = one;
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 Two
TimerTask and handler (no timer improvements)
public class TimerTask extends activity{
private int reclen = one;
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 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 Three
Handler with message (without timertask)
public class TimerTask extends activity{
private int reclen = one;
Private TextView Txtview;
public void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.timertask);
Txtview = (TextView) Findviewbyid (r.id.txttime);
Message message = Handler.obtainmessage (1); Message
handler.sendmessagedelayed (message, 1000);
}
Final Handler Handler = new Handler () {public
void Handlemessage (msg) { //Handle message
switch (m Sg.what) {case
1:
reclen--;
Txtview.settext ("" + Reclen);
if (Reclen > 0) {message message
= Handler.obtainmessage (1);
handler.sendmessagedelayed (message, 1000); Send Message
}else{
txtview.setvisibility (view.gone);
}
Super.handlemessage (msg);}}
;
Method Four
Handler and thread (does 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 (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 = new Message ();
Message.what = 1;
Handler.sendmessage (message); }catch (Exception e) {}}}}
Method Five
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 = (TextView) Findviewbyid (r.id.txttime);
Handler.postdelayed (runnable, 1000);
}
Handler Handler = new Handler ();
Runnable 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 the countdown
Method 4, Method 5, all timed
Timing and countdown can all be implemented using the above method (code changes slightly)
UI thread Comparisons
Method 1, Method 2 and Method 3, are the timings that are implemented in the UI thread;
Method 4 and Method 5, is another open runnable thread implementation timing
Comparison of Implementation methods
Method 1, using the Java implementation, that is, timer and TimerTask way;
The other four methods, all using handler message processing
Recommended Use
If the UI thread interaction requirements are not very high, you can choose Method 2 and Method 3
If you consider a UI thread blocking that seriously affects the user experience, it is recommended that you use Method 4, which is used separately for timing and other logical processing
Method 5, combining the advantages of the first several methods, is the simplest
I hope this article will help you with the Android program.