How to check times in milliseconds in android

quin :

I've got a milliseconds chronometer that works fine. Button code:

btnStart.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
            startTime = SystemClock.uptimeMillis();

                customHandler.postDelayed(updateTimerThread,0);
                txtCheck.setText("");
            }
        });

Runnable:

 Runnable updateTimerThread = new Runnable() {
    @Override
    public void run() {
    timeMilliSeconds = SystemClock.uptimeMillis()-startTime;
    updateTime = timeSwapBuff+timeMilliSeconds;
    secs = (int)(updateTime/1000);
    mins = (int)(secs/60);
    secs %=60;
    milliseconds = (int)(updateTime%1000);
    txtChrono.setText(""+mins+":"+String.format("%2d",secs)+":"
                                +String.format("%3d",milliseconds));
    customHandler.postDelayed(this,0);
      check(secs, milliseconds)  
    }
};

Function to check if time matches:

 public void check(Integer secs, long milli){
    if (secs.equals(45) && milli.equals(367)){
        txtCheck.setText("done");
    }
}

I need to check the time in order to start an event. The times I have to check are in seconds.milliseconds (i.e. 45.367), but this function (check) does not work. Any idea?

The 'check' function works... but only 1 out of five times: maybe the refreshing time is too low? Also, the code is in a fragment: does it have any influence on timer?

nulldroid :

It's likely that your calculations take longer than 1 millisecond. You should not check that often. It's expensive and not safe. Why not use a CountdownTimer? It does the job and it's based on milliseconds.

 private long[] checkTimes = new long[]{ 45367L,90446L,25384L}; 
 for (int i = 0; i < checkTimes.length; i++) {
    new CountDownTimer(checkTimes[i], 1000) {

         public void onTick(long millisUntilFinished) {
             txtCheck.setText("seconds remaining: " + millisUntilFinished / 1000);
         }

         public void onFinish() {
             txtCheck.setText("done");
         }
      }.start();
 }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=378539&siteId=1