Android is not allowed to execute UI threads in threads because of thread safety concerns.
So UI action is not allowed in the thread
The handler mechanism can be used to receive the information sent by the timer every second, or directly using the handler mechanism
1, method one: Handler+thread
PackageCom.example.yuyin_lixian;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportJava.util.Timer;ImportJava.util.TimerTask;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; /*** Handler Timer * *@authorSmalt **/ Public classMainactivityextendsActivity {TextView tvShow; Private inti = 0; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TvShow=(TextView) Findviewbyid (R.ID.TEXTVIEW1); NewThread (Newthreadshow ()). Start (); } //handler class receives dataHandler Handler =NewHandler () { Public voidhandlemessage (Message msg) {if(Msg.what = = 1) {Tvshow.settext (integer.tostring (i++)); System.out.println ("Receive ...."); } }; }; //Thread Class classThreadshowImplementsRunnable {@Override Public voidrun () {//TODO auto-generated Method Stub while(true) { Try{Thread.Sleep (1000);//execute once every secondMessage msg =NewMessage (); Msg.what= 1; Handler.sendmessage (msg); System.out.println ("Send ..."); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); System.out.println ("Thread error ..."); } } } }}
2, method Two: Handler class comes with the postdelyed
This Runnable object is called after 1 seconds using the Postdelayed method
Handler.postdelayed (runnable, 1000); It actually implements a 1s timer.
If you want to turn off this timer, you can do this
Handler.removecallbacks (runnable);
PackageCom.example.yuyin_lixian;Importandroid.app.Activity; ImportAndroid.os.Bundle; ImportAndroid.os.Handler; ImportAndroid.widget.TextView; /*** Handler timer is implemented using postdelyed * *@authorSmalt **/ Public classMainactivityextendsActivity {TextView tvShow; Private inti = 0; Private intTime = 1000; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TvShow=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Handler.postdelayed (runnable, time); //execute every 1s} Handler Handler=NewHandler (); Runnable Runnable=NewRunnable () {@Override Public voidrun () {//Handler Self-band method implementation Timer Try{handler.postdelayed ( This, time); Tvshow.settext (integer.tostring (i++)); System.out.println ("Do ..."); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); System.out.println ("Exception ..."); } } }; }
3, method two: Handler+timer+timertask
PackageCom.example.yuyin_lixian;ImportJava.util.Timer; ImportJava.util.TimerTask; Importandroid.app.Activity; ImportAndroid.os.Bundle; ImportAndroid.os.Handler; ImportAndroid.os.Message; ImportAndroid.widget.TextView; /*** Timer Implementation: Handler+timer+timertask * *@authorSmalt **/ Public classMainactivityextendsActivity {TextView tvShow; Private inti = 0; Private intTime = 1000; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TvShow=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Timer.schedule (Task,1000, 1000);//execute task after 1s and execute again after 1s} Handler Handler=NewHandler () { Public voidhandlemessage (Message msg) {if(Msg.what = = 1) {Tvshow.settext (integer.tostring (i++)); } Super. Handlemessage (msg); }; }; Timer Timer=NewTimer (); TimerTask Task=NewTimerTask () {@Override Public voidrun () {//things to do: send a messageMessage message =NewMessage (); Message.what= 1; Handler.sendmessage (message); } }; }
Android Learning Note (iv) Timer timers