eclipse - Digital clock text stopped in Android -
final textview tw = (textview) findviewbyid(r.id.textview1); string mydate = java.text.dateformat.getdatetimeinstance().format( calendar.getinstance().gettime()); tw.settext(mydate);
is android application code, text displayed hr hour stopped, why?
because, have refresh it, when want display time. code set once. can utilize textclock, or refresh code every x seconds in infinitive thread this:
private thread datetimethread; private textview tw; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); tw = (textview) findviewbyid(r.id.textview1); } @override public void onpause() { super.onpause(); if (datetimethread != null) { datetimethread.interrupt(); datetimethread = null; } } @override public void onresume() { super.onresume(); datetimerunner timerunnable = new datetimerunner(); datetimethread = new thread(timerunnable); datetimethread.start(); } private void ticktime() { runonuithread(new runnable() { public void run() { string mydate = java.text.dateformat.getdatetimeinstance().format(calendar.getinstance().gettime()); tw.settext(mydate); } }); } protected class datetimerunner implements runnable { // @override public void run() { while (!thread.currentthread().isinterrupted()) { seek { ticktime(); thread.sleep(1000); } grab (interruptedexception e) { thread.currentthread().interrupt(); } grab (exception e) { } } } }
android eclipse time
No comments:
Post a Comment