Android Alarm Clock (iv): Implement Timer

Source: Internet
Author: User

Timerview.java

 PackageCom.mytest.myclock;ImportJava.util.Timer;ImportJava.util.TimerTask;ImportAndroid.app.AlertDialog;ImportAndroid.content.Context;ImportAndroid.os.Handler;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView; Public classTimerviewextendsLinearLayout { PublicTimerview (Context context) {Super(context); //TODO auto-generated Constructor stub    }     PublicTimerview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); //TODO auto-generated Constructor stub    }     PublicTimerview (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub    }    PrivateTextView Tvhour; PrivateTextView Tvminute; PrivateTextView Tvsecond; PrivateButton btnstart; PrivateButton Btnstop; @Overrideprotected voidonfinishinflate () {//TODO auto-generated Method Stub        Super. Onfinishinflate ();    Init (); }    Private voidinit () {Tvhour= (TextView) This. Findviewbyid (R.id.timer_hour); Tvminute= (TextView) This. Findviewbyid (R.id.timer_mini); Tvsecond= (TextView) This. Findviewbyid (R.id.timer_second); Btnstart= (Button) This. Findviewbyid (R.id.btn_start_timer); Btnstop= (Button) This. Findviewbyid (R.id.btn_stop_timer); Btnstop.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {stoptimer ();        }        }); Btnstart.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {starttimer ();    }        }); }    Private Final intwork_state_stop = 0;//timekeeping status, stopping    Private Final intWork_state_run = 1;//Timing Status    Private intUserinputtime = 0; PrivateTimer timer; Privatetimertask task; PrivateHandler Handler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {Switch(msg.what) { CaseWork_state_run:inthour = USERINPUTTIME/60/60; intminute = (USERINPUTTIME/60)%60; intSecond = userinputtime%60; Tvhour.settext (""+hour); Tvminute.settext (""+minute); Tvsecond.settext (""+second);  Break;  CaseWork_state_stop:NewAlertdialog.builder (GetContext ()). Settitle ("Time to"). Setnegativebutton ("Cancel",NULL). Show ();                Stoptimer ();  Break;    }        };    }; Private voidStarttimer () {Try{userinputtime= (Integer.parseint (Tvhour.gettext (). toString ()) * * + integer.parseint (Tvminute.gettext (). Tostri NG ()) * 60 +Integer.parseint (Tvsecond.gettext (). toString ())); } Catch(Exception e) {LOG.E ("Info", "Timerview->starttimer" +e.getmessage ()); return; } Timer=NewTimer (); Task=NewTimerTask () {@Override Public voidrun () {Userinputtime--;                Handler.sendemptymessage (Work_state_run); if(Userinputtime <=0) {handler.sendemptymessage (work_state_stop);                Stoptimer ();        }                            }        }; Timer.schedule (Task,1000,1000);//delay one second and execute once every second Timertask.run ()    }    Private voidStoptimer () {if(task!=NULL) {task.cancel (); Task=NULL; }    }}
View Code

Summary:

1, using timer and timertask to refresh time;

2, the acquisition of hours should be hour = USERINPUTTIME/60/60, and not directly hour = userinputtime/360.

Android Alarm Clock (iv): Implement Timer

Related Article

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.