How to Start Incremental Timer in Android -


before asking question let me tell question has been answered many time didn't resolve problem. tried this questions answer not helpful.

so problem creating mastermind game in android , want start timer 00:00 when user enter first pin on until user hit last pin or when user click on new game timer reset , not start until user enter first pin again.

so here try

handler timerhandler = new handler(); runnable timerrunnable = new runnable() {      @override     public void run() {         long millis = system.currenttimemillis() - starttime;         int seconds = (int) (millis / 1000);         int minutes = seconds / 60;         seconds = seconds % 60;          timertextview.settext(string.format("%d:%02d", minutes, seconds));          timerhandler.postdelayed(this, 500);     } }; 

function start or stop timer

@override public void onpause() {     super.onpause();     timerhandler.removecallbacks(timerrunnable);     timercheck = true; } 

when user hit first pin code execute

timercheck = true; starttimer(); 

when user hit last pin code execute

timercheck = false; starttimer(); 

nothing working please me start timer when first condition true , stop timer when last condition true, in between if newgame hit timer reset , don't start until first pin got hit again. grateful. in advance

i solve problem , here how done it

declarations

private handler customhandler = new handler(); private long starttime = 0l; long timeinmilliseconds = 0l; long timeswapbuff = 0l; long updatedtime = 0l; 

to start timer when first pin enter

starttime = systemclock.uptimemillis(); customhandler.postdelayed(updatetimerthread, 0); 

pause timer when last pin enters

timeswapbuff += timeinmilliseconds; customhandler.removecallbacks(updatetimerthread); 

main runnable

private runnable updatetimerthread = new runnable() {      public void run() {          timeinmilliseconds = systemclock.uptimemillis() - starttime;          updatedtime = timeswapbuff + timeinmilliseconds;          secs = (int) (updatedtime / 1000);         mins = secs / 60;         secs = secs % 60;         int milliseconds = (int) (updatedtime % 1000);         timertextview.settext(string.format("%02d", mins) + ":"                 + string.format("%02d", secs));         customhandler.postdelayed(this, 0);     }  }; 

its totally working fine me hope work guys too


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -