Android handler usage

Source: Internet
Author: User

Android handler usage

Handler can perform some operations at intervals. The demo function below shows different data in the myitems array every 3 seconds.

 
Public class mainactivity extends activity {private int COUNT = 0; Private Static final string [] myitems = {"here is a Item1", "Here is a item2 ", "Here is a item3", "Here is a item4"}; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); timer = new timer (); timer. scheduleatfixedrate (New newtask (), 10,300 0);} private handler hand = new handler () {public void handlemessage (Message MSG) {Switch (MSG. what) {Case 1: Toast. maketext (mainactivity. this, myitems [count], toast. length_short ). show (); If (COUNT = 3) Count = 0; elsecount ++; break ;};}; private class newtask extends timertask {@ overridepublic void run () {// todo auto-generated method stubmessage MSG = new message (); MSG. what = 1; hand. sendmessage (MSG );}}}

 

The followingArticleDetailed explanation:

Android handler usage Summary

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.