-1

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);
  • Doesn't your timer calculate the start time originally from 0 (zero) as you defined it in declaration, but in your button code you set the starTime to .uptimeMillis(), which is the time since the system boot? Naturally it should show random numbers as you calculate not from zero but from random value since you booted? – Stacking For Heap Mar 02 '18 at 07:25
  • @StackingForHeap Thank you to pay attention at my post :). I think the declaration I made is to declare the initial value, but when the button triggered, the value start from zero. If the `starttime = SystemClock.uptimeMillis` removed, the stopwatch generate random numbers – Andhika Brosnan Mar 02 '18 at 07:42
  • Did you reset the price-value to 3000 as well before handler.PostDelayed? – Stacking For Heap Mar 02 '18 at 07:55
  • @StackingForHeap Yes I did, I reset the price-value before handler.PostDelayed executed :( – Andhika Brosnan Mar 02 '18 at 09:35
  • That does not show in your code, also you are not showing if you remembered to reset timeSwapBuff -variable, which you add to timeinMilliseconds? If that is not reset, you get extra seconds and start values >3000. – Stacking For Heap Mar 02 '18 at 19:01
  • It still seems to me that your basis for the price is set by calculating timeinMilliseconds equals "SystemClock.uptimeMillis() - start-time", right? Why then are you expecting to get different results as the SystemClock.uptimeMillis() is an ongoing timer that calculates the time since the phone/device/os was last booted? It will get higher/larger forever till the moment you restart again (not your program, but whole device)? Also if you depress the button, startTime gets set back to starting value (zero), so "SystemClock.uptimeMillis() minus zero" is what you would get, correct? – Stacking For Heap Mar 04 '18 at 22:20
  • Whoaa thank you so much @StackingForHeap God Bless You – Andhika Brosnan Mar 05 '18 at 03:50
  • I'm glad that it got solved, was starting to doubt my code reading skills. Been ever since I did any native android or java even. – Stacking For Heap Mar 05 '18 at 03:51
  • Actually when I reset the startTime, the stopwatch works good because I reset StartTime to zero before the "startTime = SystemClock.uptimeMillis()" executed – Andhika Brosnan Mar 05 '18 at 04:10
  • I'm sorry it's not done yet :( The bug is still there, and I still figuring out the algorythm, anyway your argument helps me a lot thanks – Andhika Brosnan Mar 05 '18 at 04:12

1 Answers1

0

It seems that the Price still increasing because the "price % 30 == 0" logic. My bad. When the stopwatch the secs turn 0 which %30 is also 0. Therefore, it's not go to initial value. I've changed the logic, that I make a new a new Runnable for price.