0

This is my code:

public class BFragment extends SherlockFragment {

private Button start;
private View v;
private Button reset;
private Button pause;
private TextView hourTextView;
private TextView minTextView;
private TextView secTextView;
private int secondCounter;
private int minuteCounter;
private int hourCounter;
private Thread timerThread;
private boolean continueThread = false;
private boolean isRunning = false;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    v = inflater.inflate(R.layout.activity_bfragment, container, false);
    start = (Button) v.findViewById(R.id.start); 
    pause = (Button) v.findViewById(R.id.pause);
    reset = (Button) v.findViewById(R.id.reset);
    hourTextView = (TextView) v.findViewById(R.id.hh);
    minTextView = (TextView) v.findViewById(R.id.mm);
    secTextView = (TextView) v.findViewById(R.id.ss);



    start.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            if (isRunning) {

            } else {
                continueThread = true;
                timeUpdate();   
                isRunning = true;
                isRunning = true;
            }
        }
    });

    pause.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            continueThread = false;
            isRunning = false;
            start.setText("Resume");
            isRunning = false;
        }
    });

    reset.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            hourTextView.setText("00");
            minTextView.setText("00");
            secTextView.setText("00");
            hourCounter = 0;
            secondCounter = 0;
            minuteCounter = 0;
            continueThread = false;
            isRunning = false;
        }
    });


    return v;

}

final Handler mHandlerUpdateSec = new Handler();
final Runnable mUpdateSec = new Runnable() {
    public void run() {
        String temp = "" + secondCounter;
        System.out.println("Temp second counter length: " + temp.length());
        if(temp.length() == 1)
            secTextView.setText("0" + secondCounter);
        else
            secTextView.setText("" + secondCounter);
    }
};
final Handler mHandlerUpdateMinute = new Handler();
final Runnable mUpdateMinute= new Runnable() {
    public void run() {
        String temp = "" + minuteCounter;
        System.out.println("Temp second counter length: " + temp.length());
        if(temp.length() == 1)
            minTextView.setText("0" + minuteCounter);
        else
            minTextView.setText("" + minuteCounter);
    }
};
final Handler mHandlerUpdateHour = new Handler();
final Runnable mUpdateHour = new Runnable() {
    public void run() {
        String temp = "" + hourCounter;
        System.out.println("Temp second counter length: " + temp.length());
        if(temp.length() == 1)
            hourTextView.setText("0" + hourCounter);
        else
            hourTextView.setText("" + hourCounter);
    }
};

public void timeUpdate()
{
    timerThread = new Thread(new Runnable() {

        public void run() {
            while(continueThread){
                Date newDate = new Date();
                Date date = new Date();
                System.out.println(newDate.getTime() - date.getTime());
                if(true){
                    secondCounter = secondCounter+1;
                    mHandlerUpdateSec.post(mUpdateSec);
                    System.out.println("Inside the Theread ..."+secondCounter);
                    if(secondCounter > 59){
                        minuteCounter = minuteCounter + 1;
                        mHandlerUpdateMinute.post(mUpdateMinute);
                        secondCounter = 0;
                        if(minuteCounter > 59){
                            hourCounter = hourCounter + 1;
                            mHandlerUpdateHour.post(mUpdateHour);
                            minuteCounter = 0;
                        }
                    }
            }
        }
    });
    timerThread.start();
}
}

If the user would press Start/Pause really fast (nearly together) then the chrono counts fast up and is skipping seconds... It doesn't stop until pause is pressed then it works normally again? Why's that and how to prevent it?

Thanks!

moritzg
  • 4,266
  • 3
  • 37
  • 62

1 Answers1

0

For starters, there's no kind of delay in your code. Your timeUpdate() basically is doing this:

while(continueThread){
    secondCounter = secondCounter+1;
    // ... display the counter ...
}

You need to do something to tie your app to the real-time clock. A straightforward approach would be to calculate the elapsed time every pass through the loop and display that. An easy optimization would be to put some checks in your code to see if things have changed. To be still more efficient, you can make your thread sleep instead of spinning.

I would like to suggest, though, that rather than focusing on redrawing the display every time the counter changes, instead turn it around. Wait and do nothing until, in human terms, it's about time for your user to see an update. (This could be every second, every 10th of a second, whatever.) When it's about time to show your user a new update, then look at your timestamps, measure the elapsed time, and write it to your display.

Sparky
  • 8,437
  • 1
  • 29
  • 41