Android Handler Timer timertask combination for timed refresh UI

Source: Internet
Author: User

Code implements a small timer

Format similar to 00:01:00

ImportJava.util.Timer;ImportJava.util.TimerTask;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.widget.TextView; Public classMainactivityextendsactivity{TextView TextView;        Timer timer; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TextView=(TextView) Findviewbyid (R.id.text); Timer=NewTimer (); Timer.schedule (Task,1000,1000); }    FinalHandler Handler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case1: Textview.settext (Msg.obj.toString ());  Break; }            Super. Handlemessage (msg);        }    }; LongTime = 0; TimerTask Task=NewTimerTask () {@Override Public voidrun () {Message msg=NewMessage (); Msg.what= 1; Msg.obj=Formatelapsedtime (time); time++;        Handler.sendmessage (msg);        }    }; /*** Formatted output time String *@paramElapsedseconds *@return     */     Public StaticString Formatelapsedtime (Longelapsedseconds) {        LongHours = 0; Longminutes = 0; Longseconds = 0; if(Elapsedseconds >= 3600) {hours= elapsedseconds/3600; Elapsedseconds-= hours * 3600; }        if(Elapsedseconds >= 60) {minutes= ELAPSEDSECONDS/60; Elapsedseconds-= minutes * 60; } seconds=elapsedseconds; StringBuilder SB=NewStringBuilder ();        Sb.append (Addzeroifneed (hours)); Sb.append (":");        Sb.append (Addzeroifneed (minutes)); Sb.append (":");        Sb.append (Addzeroifneed (seconds)); returnsb.tostring (); }        /*** Plus 0 processing, less than two bit 0 *@paramNumber *@return     */     Public StaticString Addzeroifneed (LongNumber ) {StringBuilder SB=NewStringBuilder (); if(number >= 0 && number <= 9){            returnSb.append ("0"). Append (number). ToString (); }        returnsb.append (number). ToString (); }}

Do not stop processing, only for reference,

Android Handler Timer timertask combination for timed refresh UI

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.