android - How to view,compare and refresh current time? -
the aim of current application current time of phone , compare list of timerange i've set , view time remaining between current time , time range ( need refresh of view each seconds ).
example : current time = 11 pm time range = 11:10 pm
i view :
x = 11h = 11h10min view ( x); // refreshing each seconds view ( a- x); // refreshing each seconds
for moment suceeded view current time :
public class horairetempsreel extends activity { private textview tvdisplaytime; private int hour; private int minute; private string curtime; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.horairetempsreel); setcurrenttimeonview(); } // display current time public void setcurrenttimeonview() { tvdisplaytime = (textview) findviewbyid(r.id.tvtime); final calendar c = calendar.getinstance(); hour = c.get(calendar.hour_of_day); minute = c.get(calendar.minute); // set current time textview resources curtimeres = getresources(); curtime = curtimeres.getstring(r.string.curtime,hour,minute); tvdisplaytime.settext(curtime); }
}
but didn't suceed refreshed every seconds. i've done researchs , find out using handlers may solution concept little bit hard understand ( i'm beginner in android dev ) know if there way resolve problem.
( sorry bad english , i'm not native )
update : answer salauyou !
first, bad style keep date-time variables in ints or strings. there specialized class in java called date purpose.
ok try change possible .
and don't rely on default locale!
what mean ?
now timer scheduled , execute setcurrenttimeonview() method every 1 second until cancel timer.cancel()
ok tried adapt code give me program , gives :
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.horairetempsreel); timer timer = new timer(); timer.schedule(new timertask(){ // set timertask @override public void run(){ // override run() method setcurrenttimeonview(); } }, 0, 1000); }
i don't know put timer.cancel() , gives me error on logcat :
e/androidruntime(7701): android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views.
Comments
Post a Comment