Android Timer Timer Usage

Source: Internet
Author: User

Last night in writing an Android timer, began to think it is very easy, using Java general wording, the results found that the timer can not run at all, but did not report any errors, then stayed there, do not know how to do, and later on the online check, Android for thread safety issues, Do not allow UI threads to be executed in the thread (as I now know, there are other limitations found), there is an interesting class in Android: Android.os.Handler, which enables message delivery across threads.
First look at the segment code, this instantiation of a handler,handler can communicate through a message on multiple threads, I do this is timed Reclen plus 1, and then in a certain format, displayed on the Rectime (UI thread action).

Code
1 FinalHandler Handler= NewHandler () {
2 Public voidhandlemessage (Message msg) {
3 Switch(msg.what) {
4 Case 1:
5 Reclen++;
6 Rectime.settext (Getrectime (Reclen));
7 Break;
8 }
9 Super. Handlemessage (msg);
Ten }
One };


The following is an instantiation of a timertask, which is to provide a timer to execute the content. What I add in this method is to give handler a Send Message function, because it cannot manipulate the UI thread directly in the thread of the timer.

1 timertask Task= NewTimerTask () {
2 Public voidRun () {
3 Message Message= NewMessage ();
4 Message.what= 1;
5 Handler.sendmessage (message);
6 }
7 };


The rest of the work is much simpler, adding a timer to get the program up and running.

1 Timer= NewTimer (true);
2 timer.schedule (Task, +, +); //delay 1000ms After execution, 1000ms execution once
3 //Timer.cancel ();//Exit Timer


Speaking here, perhaps some still feel foggy, long time did not write things, also do not know how to put the problem to say more understand, this again posted on my wrong way of writing, contrast wrong writing, perhaps can let everyone quickly understand come over

wrong wording
1timertask Task= NewTimerTask (){
2 Public voidRun (){
3 Reclen ++ ;
4 Rectime.settext (Getrectime (Reclen)); // operate the UI thread directly in the TimerTask, and when you step through it, the program is never executed here .
5 }
6};
7Timer= NewTimer (true);
8timer.schedule (Task, +, +);


Note: I am also an Android beginner, said the wrong place to welcome you to correct, very much want to communicate here and everyone, a learning process encountered problems when feeling really helpless.

Android Timer Timer Usage

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.