Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

Android: chronometer as a persistent stopwatch. How to set starting time? What is Chronometer "Base"?

I do have one service running in the background. Whenever it starts I store in memory the starting time in milliseconds:

startingTime = new Date().getTime();

I want to display a chronometer that starts counting when the service starts and never stops until the user presses a button. I want to allow the user to leave the activity rendering the chronometer, do some stuff and then return. But the idea is that when the user returns I dont want the chronometer to go to 0:00 again. Insted I want it to show the exact time that has passed ever since the service has started.

I can calculate elapsedTime every time the user return to the chronometer activity:

elapsedTime =  new Date().getTime() - startingTime;

The thing is that i dont know how to tell the chronometer to start counting from that time!

Setting it as the chronometer base does not work. Can someon explain what exactly "base" means or how to accomplish this?

thanks a lot! BYE

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use Chronometer.

You should also check this thread.

EDIT: The solution:

public class ChronoExample extends Activity {
 Chronometer mChronometer;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     LinearLayout layout = new LinearLayout(this);
     layout.setOrientation(LinearLayout.VERTICAL);

     mChronometer = new Chronometer(this);

     // Set the initial value
     mChronometer.setText("00:10");
     layout.addView(mChronometer);

     Button startButton = new Button(this);
     startButton.setText("Start");
     startButton.setOnClickListener(mStartListener);
     layout.addView(startButton);

     Button stopButton = new Button(this);
     stopButton.setText("Stop");
     stopButton.setOnClickListener(mStopListener);
     layout.addView(stopButton);

     Button resetButton = new Button(this);
     resetButton.setText("Reset");
     resetButton.setOnClickListener(mResetListener);
     layout.addView(resetButton);        

     setContentView(layout);
 }

 private void showElapsedTime() {
     long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();            
     Toast.makeText(ChronoExample.this, "Elapsed milliseconds: " + elapsedMillis, 
             Toast.LENGTH_SHORT).show();
 }

 View.OnClickListener mStartListener = new OnClickListener() {
     public void onClick(View v) {
      int stoppedMilliseconds = 0;

         String chronoText = mChronometer.getText().toString();
         String array[] = chronoText.split(":");
         if (array.length == 2) {
           stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
               + Integer.parseInt(array[1]) * 1000;
         } else if (array.length == 3) {
           stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000 
               + Integer.parseInt(array[1]) * 60 * 1000
               + Integer.parseInt(array[2]) * 1000;
         }

         mChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
         mChronometer.start();
     }
 };

 View.OnClickListener mStopListener = new OnClickListener() {
     public void onClick(View v) {
         mChronometer.stop();
         showElapsedTime();
     }
 };

 View.OnClickListener mResetListener = new OnClickListener() {
     public void onClick(View v) {
         mChronometer.setBase(SystemClock.elapsedRealtime());
         showElapsedTime();
     }
 };
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...