android - Custom Count Up Timer -
i need count timer in application. browsed many forums subject, not find anything. understood can chronometer, have 2 problem chronometer:
- i cannot using chronometer in service because chronometer needs layout.
- i cannot initialize chronometer count more 1 hour.
my code:
stopwatch = new chronometer (mainactivity.this); starttime = systemclock.elapsedrealtime(); stopwatch.start(); stopwatch.setonchronometerticklistener(new chronometer.onchronometerticklistener() { @override public void onchronometertick(chronometer arg0) { countup = (systemclock.elapsedrealtime() - arg0.getbase()) / 1000; string astext = (countup / 60) + ":" + (countup % 60); log.i("t", astext); } });
you can use countdowntimer in reverse , time elapsed.
long totalseconds = 30; long intervalseconds = 1; countdowntimer timer = new countdowntimer(totalseconds * 1000, intervalseconds * 1000) { public void ontick(long millisuntilfinished) { log.d("seconds elapsed: " , (totalseconds * 1000 - millisuntilfinished) / 1000); } public void onfinish() { log.d( "done!", "time's up!"); } };
to start timer.
timer.start();
to stop timer.
timer.cancel();
Comments
Post a Comment