java - How can i make that in the timer my program will do something every second? -
in top of mainactivity.java added:
private handler customhandler = new handler(); long timeswapbuff = 0l;
then inside oncreate did:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); starttime = systemclock.uptimemillis(); customhandler.postdelayed(updatetimerthread,0); timervalue = (textview) findviewbyid(r.id.timervalue); startbutton = (button) findviewbyid(r.id.startbutton); startbutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { starttime = systemclock.uptimemillis(); customhandler.postdelayed(updatetimerthread, 0); } }); pausebutton = (button) findviewbyid(r.id.pausebutton); pausebutton.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { customhandler.removecallbacks(updatetimerthread); } });
then in updatetimerthread:
private runnable updatetimerthread = new runnable() { public void run() { long updatedtime = 0l; long timeinmilliseconds = 0l; timeinmilliseconds = systemclock.uptimemillis() - starttime;//system.currenttimemillis() - starttime; updatedtime = timeswapbuff + timeinmilliseconds; int secs = (int) (updatedtime / 1000); int mins = secs / 60; secs = secs % 60; int milliseconds = (int) (updatedtime % 1000); timervalue.settext("" + mins + ":" + string.format("%02d", secs) + ":" + string.format("%03d", milliseconds)); customhandler.postdelayed(this, 0); } };
now each time run program start timer start 00:00:00
now want add somehow each second something. not reset timer not pause or stop let keep running each second want else in program.
the problem how check every second ?
Comments
Post a Comment