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