android - countdowntimer skipping some milliseconds -
as can see last call ontick happening 2 seconds, next call 2 seconds later.
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); tv = new textview(this); this.setcontentview(tv); simpledateformat formatter = new simpledateformat("dd.mm.yyyy, hh:mm:ss:sss"); formatter.setlenient(false); newtime = "31.12.2015, 23:59:59:999"; try { newdate = formatter.parse(newtime); milliseconds = newdate.gettime(); currenttime = system.nanotime(); diff = milliseconds - currenttime; } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } mycount counter = new mycount(milliseconds, 1000); counter.start(); }
countdowntimer
public class mycount extends countdowntimer { public mycount(long millisinfuture, long countdowninterval) { super(millisinfuture, countdowninterval); } @override public void onfinish() { } @override public void ontick(long millisuntilfinished) { serveruptimeseconds = (millisuntilfinished - system .currenttimemillis()) / 1000; string serveruptimetext = string.format( "%d days %d hours %d minutes %d seconds", serveruptimeseconds / 86400, (serveruptimeseconds % 86400) / 3600, ((serveruptimeseconds % 86400) % 3600) / 60, ((serveruptimeseconds % 86400) % 3600) % 60); tv.settext(serveruptimetext); } }
output:
10 seconds 8 seconds 6 seconds 4 seconds
p.s ~ check video more info
try out :
timertask runtimertask = new timertask() { @override public void run() { // code }); }; timer timer = new timer(); timer.scheduleatfixedrate(runtimertask, 0, 500);
Comments
Post a Comment