Android three ways to implement timers _android

Source: Internet
Author: User

Method one, using handler and thread (thread) to implement the timer

Copy Code code as follows:

Package Com.xunfang.handerDemo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.widget.TextView;
public class Handerdemoactivity extends activity {
TextView tvshow;
private int i = 0;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Tvshow = (TextView) Findviewbyid (r.id.tv_show);
New Thread (New Threadshow ()). Start ();
}
Handler class receives data
Handler Handler = new Handler () {
public void Handlemessage (msg) {
if (msg.what = = 1) {
Tvshow.settext (integer.tostring (i++));
SYSTEM.OUT.PRINTLN ("Receive ...");
}
};
};
Thread class
Class Threadshow implements Runnable {
@Override
public void Run () {
while (true) {
try {
Thread.Sleep (1000);
msg = new Message ();
Msg.what = 1;
Handler.sendmessage (msg);
SYSTEM.OUT.PRINTLN ("Send ...");
catch (Exception e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Thread error ...");
}
}
}
}
}

Method Two, use the handler class to carry postdelyed to implement the timer

Copy Code code as follows:

Package Com.xunfang.handerDemo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.widget.TextView;
public class Handerdemoactivity extends activity {
TextView tvshow;
private int i = 0;
private int time = 1000;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Tvshow = (TextView) Findviewbyid (r.id.tv_show);
Handler.postdelayed (runnable, time); every 1s execution
}
Handler Handler = new Handler ();
Runnable Runnable = new Runnable () {
@Override
public void Run () {
Realization of timer by handler self-band method
try {
Handler.postdelayed (this, time);
Tvshow.settext (integer.tostring (i++));
System.out.println ("do ...");
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Exception ...");
}
}
};
}

Method Three, use handler, timer and TimerTask three Android class implementation timer

Copy Code code as follows:

Package Com.xunfang.handerDemo;
Import Java.util.Timer;
Import Java.util.TimerTask;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.widget.TextView;
public class Handerdemoactivity extends activity {
TextView tvshow;
private int i = 0;
private int time = 1000;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Tvshow = (TextView) Findviewbyid (r.id.tv_show);
Timer.schedule (Task, 1000, 1000); Execute task after 1s, execute again after 1s
}
Handler Handler = new Handler () {
public void Handlemessage (msg) {
if (msg.what = = 1) {
Tvshow.settext (integer.tostring (i++));
}
Super.handlemessage (msg);
};
};
Timer timer = new timer ();
TimerTask task = new TimerTask () {
@Override
public void Run () {
Send a message
Message message = new Message ();
Message.what = 1;
Handler.sendmessage (message);
}
};
}

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.