Public thread_time;
Private int time = 8;
Final handler = new handler () {public void handlemessage (Message MSG) {Switch (MSG. what) {Case 1: Time --; timeview. settext ("" + time); If (time = 0) {winorlose (butid, computerid); // compare timeview with two IDs. settext ("" + time); thread_time.setflag (false);} If (time <0) {time = 0; timeview. settext ("" + time) ;}break; default: Break ;}}};
Public void oncreate (bundle savedinstancestate) {requestwindowfeature (window. feature_no_title); super. oncreate (savedinstancestate); setcontentview (R. layout. main); // start the thread thread_time = new thread_time (gameactivity. this); thread_time.start ();}
public class Thread_Time extends Thread{ GameActivity GA = new GameActivity(); public boolean flag = true;public void setFlag(boolean flag) {this.flag = flag;}public Thread_Time(GameActivity GA) {this.GA = GA;} public void run(){ while(flag){ try{ Thread.sleep(1000); // sleep 1000ms Message message = new Message(); message.what = 1; GA.handler.sendMessage(message); }catch (Exception e) { e.printStackTrace(); } } } }