How to implement timers in Android

Source: Internet
Author: User

First, Handler and Thread

 PackageCom.lstech.app;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.widget.TextView;/*** Handler Timer * *  */ Public classHandleractivityextendsActivity {TextView tvShow; Private inti = 0; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); TvShow=(TextView) Findviewbyid (r.id.tv_show); NewThread (Newthreadshow ()). Start (); }    //handler class receives dataHandler Handler =NewHandler () { Public voidhandlemessage (Message msg) {if(Msg.what = = 1) {Tvshow.settext (integer.tostring (i++)); System.out.println ("Receive ....");    }        };    }; //Thread Class    classThreadshowImplementsRunnable {@Override Public voidrun () {//TODO auto-generated Method Stub             while(true) {                Try{Thread.Sleep (1000); Message msg=NewMessage (); Msg.what= 1;                    Handler.sendmessage (msg); System.out.println ("Send ..."); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); System.out.println ("Thread error ..."); }            }        }    }}

Second, the Postdelyed method of handler

Package
Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.widget.TextView;/*** Handler timer is implemented using postdelyed *  */ Public classPostdelyedactivityextendsActivity {TextView tvShow; Private inti = 0; Private intDelyed= 1000; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); TvShow=(TextView) Findviewbyid (r.id.tv_show); Handler.postdelayed (runnable, delyed); //execute every 1s} Handler Handler=NewHandler (); Runnable Runnable=NewRunnable () {@Override Public voidrun () {//Handler Self-band method implementation Timer            Try{handler.postdelayed ( This, delyed); Tvshow.settext (integer.tostring (i++)); System.out.println ("Do ..."); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); System.out.println ("Exception ..."); }        }    };}

Third, Handler + Timer + timertask

 PackageCom.lstech.app;ImportJava.util.Timer;ImportJava.util.TimerTask;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.widget.TextView;/*** Timer Implementation * *  */ Public classTimeractivityextendsActivity {TextView tvShow; Private inti = 0; Private intTime = 1000; @Override Public voidonCreate (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 and execute again after 1s} Handler Handler=NewHandler () { Public voidhandlemessage (Message msg) {if(Msg.what = = 1) {Tvshow.settext (integer.tostring (i++)); }            Super. Handlemessage (msg);    };    }; Timer Timer=NewTimer (); TimerTask Task=NewTimerTask () {@Override Public voidrun () {//things to do: send a messageMessage message =NewMessage (); Message.what= 1;        Handler.sendmessage (message); }    };}

How to implement timers in Android

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.