Beg you pardon I'm a new developer.
I am using SystemClock.uptimeMillis
inside of Runnable
to make it look like stopwatch. I took the stopwatch source code from google.
Afterwards I make a modification every 30 seconds a variable (named price) will increase.
The weird thing is, when I start over the stopwatch, the price won't return to initial value. I've tried reset the price value in onActivityResult
but the stopwatch displayed random numbers. How to make the price variable return to initial value?
Here are my codes:
Handler handler = new Handler();
long startTime = 0L, timeinMilliseconds=0L,timeSwapBuff=0L, updateTime=0L;
int pressCounter = 0;
int price = 3000;
Runnable updateTimerThread = new Runnable() {
@SuppressLint({"DefaultLocale", "SetTextI18n"})
@Override
public void run() {
timeinMilliseconds = SystemClock.uptimeMillis()-startTime;
updateTime = timeSwapBuff+timeinMilliseconds;
int secs = (int)(updateTime/1000);
int mins = secs/60;
int hours = mins/60;
secs%=60;
int milliseconds = (int)(updateTime%1000);
tv_timer.setText(String.format("%2d",hours)+":"+String.format("%2d",mins)+":"+String.format("%2d",secs)+":"
+String.format("%3d",milliseconds));
if (secs % 30 == 0){
price++;
tv_biaya.setText("Rp. "+price+",00");
}
handler.postDelayed(this,0);
}
};
and when the button clicked the method trigger this code
startTime = SystemClock.uptimeMillis();
handler.postDelayed(updateTimerThread,0);