Run Code Over And Over
In my application I am showing a clock in a TextView, and I want to update it in real time. I tried to run it like this: public void clock() { while(clock_on == true) {
Solution 1:
Please Try:
privateHandlerhandler=newHandler();
runnable.run();
privateRunnablerunnable=newRunnable()
{
publicvoidrun()
{
//// Do the stuff//if(clock_on == true) {
executeClock();
}
handler.postDelayed(this, 1000);
}
};
Solution 2:
Use a Handler:
privateHandlerhandler=newHandler() {
@OverridepublicvoidhandleMessage(android.os.Message msg) {
TextViewtimeTv= (TextView) findViewById(R.id.time);
long currentTime=System.currentTimeMillis();
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(currentTime);
String showTime=String.format("%1$tI:%1$tM %1$Tp",cal);
timeTv.setText(showTime);
handler.sendEmptyMessageDelayed(0, 1000);
}
};
Post a Comment for "Run Code Over And Over"