Android: Chronometer SetBase in minutes

2019-08-24 00:44发布

问题:

Is there a way by which using the android chronometer class to set base of the chronometer in 15 minutes and from that period the times goes down until 0 seconds?

I have tried with setBase(60000) but this isn't work.

回答1:

In general the chronometer works like this (if you would like to set the Base to a specific nr):

mChronometer.setBase(SystemClock.elapsedRealtime() - (nr_of_min * 60000 + nr_of_sec * 1000)))

what you are asking can be done through a countdown (http://developer.android.com/reference/android/os/CountDownTimer.html)

Or create your own countdown by using the chronometer like this (more work should be done cause i just wrote this and did not test it yet)

private OnChronometerTickListener countUp = new OnChronometerTickListener(){
@Override
public void onChronometerTick(Chronometer chronometer){
            long elapsedTime = (SystemClock.elapsedRealtime() - mChronometerCountUp.getBase()) / 60000; 
            Log.v("counting up", elapsedTime); 
            // you will see the time counting up
            count_down--; 
                if(count_down == 0){
                mChronometerCountUp.stop();
            }
            // an int which will count down, 
            // this is not (very) accurate due to the fact that u r using the update part of the chronometer 
            // u just might implement the countdown i guess 
            // or 2 chronometers (one counting up and an other counting down using the elapsed time :p)
            // just remember programming is creating ur solution to problems u face its like expression urself
        };
    };


回答2:

Check out this thread Android: chronometer as a persistent stopwatch. How to set starting time? What is Chronometer "Base"? as well as this thread Android - Get time of chronometer widget. Neither answers your question directly, but the nuggets there should lead you to an answer.



回答3:

http://developer.android.com/reference/android/widget/Chronometer.html

For set the base time you can use elapsedRealtime(), and you can output format with setFormat()