Android Timer Schedule () method timed cycle switch picture

Source: Internet
Author: User

void Java.util.Timer.schedule (timertask task, long delay, long period)
The first parameter, which is the TimerTask class, is in the package: import Java.util.TimerTask. The consumer inherits the class and implements the public void run () method, because the TimerTask class implements the Runnable interface.
The second parameter means that after the user calls the schedule () method, it takes such a long time to execute the run () method for the first time.
The third parameter means that after the first call, the run () method is called once every few hours from the second start.

Then use this method to implement the picture timing cycle switch, the code is as follows:

1 ImportJava.util.Timer;2 ImportJava.util.TimerTask;3 4 Importandroid.app.Activity;5 ImportAndroid.os.Bundle;6 ImportAndroid.os.Handler;7 ImportAndroid.os.Message;8 ImportAndroid.widget.ImageView;9 Ten  Public classHandleractivityextendsActivity { One //to switch photos, place them under the Drawable folder A int[] Images ={r.drawable.img1, R.drawable.img2, R.DRAWABLE.IMG3,}; - //Message Delivery Flag - intSign = 17; the //Photo Index - intnum = 0; -  - @Override +  Public voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.main); A FinalImageView image =(ImageView) Findviewbyid (r.id.image); at FinalHandler Handler =NewHandler () { - @Override -  Public voidhandlemessage (Message msg) { - //TODO auto-generated Method Stub - Super. Handlemessage (msg); - if(Msg.what = =Sign ) { inImage.setimageresource (images[num++]); - if(Num >=images.length) { tonum = 0; + } - } the } * }; $ NewTimer (). Schedule (NewTimerTask () {Panax Notoginseng @Override -  Public voidrun () { the //TODO auto-generated Method Stub +Message msg =NewMessage (); AMsg.what =Sign ; the handler.sendmessage (msg); + } -}, 1000, 500); $ } $}

Android Timer Schedule () method timed cycle switch picture

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.