user interface - Android: Show elapsed time on UI for given start time and continue to update it with handler (timer) -
i project start time web service , work out time project had taken current date. store datetime web in starttimelist.
here how i'm getting current elapsed time on project:
public void settimeelapsed() { try { calendar calstart = calendar.getinstance(); simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); calstart.settime(sdf.parse(starttimelist.get(0))); long startmillis = calstart.gettimeinmillis(); long = system.currenttimemillis(); long difference = - startmillis; calendar caldiff = calendar.getinstance(); caldiff.settimeinmillis(difference); int eyear = caldiff.get(calendar.year); int emonth = caldiff.get(calendar.month); int eday = caldiff.get(calendar.day_of_month); int ehour = caldiff.get(calendar.hour_of_day); int emin = caldiff.get(calendar.minute); int esec = caldiff.get(calendar.second); mimduration.settext(string.format("%d months %d days %d:%d:%d", emonth, eday, ehour, emin, esec)); } catch (parseexception e) { log.e(tag, "error: " + e.getmessage()); } }
however not keep updating elapsed time user see real time. need add handler (timer) of sorts keep updating time on ui.
any appreciated. thank you.
you can create timertask , use timer schedule periodically. inside run method can update ui either using runonuithread method or handler.
so
timer.scheduleatfixedrate(new timertask() { @override public void run() { long spenttime = system.currenttimemillis() - starttime; runonuithread(new runnable() { @override public void run() { } }); } },0, interval);
Comments
Post a Comment