views:

30

answers:

0

Hello friends.

I am struggling to implement a chronometer in my application. There are two situations in which I want to use it:

  1. if starts for the first time, it simply increment the counter
  2. if resumes, it must display time since a date time (that I retrieve from the database) and increment it from there.

To be more precise.

  1. If I start the first time I will have 00:00:01, 00:00:02, 00:00:03...
  2. If I resume, I take a record from database, let's say 30 minutes old and chronometer will have 00:30:01, 00:30:02... etc

The first part is done

    chrono.setBase(SystemClock.elapsedRealtime());
    chrono.start();

    chrono=(Chronometer)findViewById(R.id.chrono);
            chrono.setText("--:--:--");
            chrono.setOnChronometerTickListener(new OnChronometerTickListener()
            {
                   public void onChronometerTick(Chronometer arg0) {
                       if (status!=Constants.Continue)
                       {
                           String HH =((elapsedTime / 3600) < 10 ? "0" : "") + (elapsedTime / 3600);
                           String MM =((elapsedTime / 60) < 10 ? "0" : "") + (elapsedTime / 60); 
                           String SS =((elapsedTime % 60) < 10 ? "0" : "") + (elapsedTime % 60);
                           currentTime = HH+":"+MM+":"+SS; 

                           elapsedTime = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
                       }
                       else 
                       {

                          //here I have startDate.getTime() as a Date variable
                           String HH =????
                           String MM =???
                           String SS =???
                           currentTime = HH+":"+MM+":"+SS; 

                          elapsedTime=elapsedTime+1000;

                       }

                       arg0.setText(currentTime);
                   }
            }
            );

It should be very simple, but I don't find the solution for the else branch. Any help is really appreciated. Thank you

Lated edit: I managed to find the difference since startdate and current time, but now the chronometer doesn't increment the seconds. Here is the updated code:

long elapsedTime = new Date().getTime() - startDate.getTime();
chrono.setBase(SystemClock.elapsedRealtime()-elapsedTime);
            chrono.start();

 chrono.setOnChronometerTickListener(new OnChronometerTickListener()
        {
               public void onChronometerTick(Chronometer arg0) {

                   int days = (int)Math.floor( elapsedTime/86400000 );

                   long diff2 = elapsedTime - days*86400000;

                   int hours = (int)Math.floor( diff2/3600000 );

                   long diff3 = diff2 - hours*3600000;

                   int min = (int)Math.floor( diff3 /60000 );

                   int sec = (int)Math.floor(( diff3 - min*1000)/60000 );


                   currentTime = days+":"+hours+":"+min+":"+sec;


                   elapsedTime = (SystemClock.elapsedRealtime() - arg0.getBase());
                   arg0.setText(currentTime);
               }
        }
        );

What am I missing ?