安卓:天文台作为一个持续的秒表。如何设置启动时间?什么是天文台表"基地"? [英] Android: chronometer as a persistent stopwatch. How to set starting time? What is Chronometer "Base"?

查看:217
本文介绍了安卓:天文台作为一个持续的秒表。如何设置启动时间?什么是天文台表"基地"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在后台运行一个服务。每当我开始在内存中存储以毫秒为单位的开始时间:

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();

我想显示天文台表的开始计数,当服务启动和永不停歇,直到用户presses一个按钮。我想允许用户离开活动呈现的计时表,做一些东西,然后返回。但这个想法是,当用户返回我不想记时计去到晚上12点了。 insted的我想让它显示已经通过自从该服务已启动的确切时间。

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.

我每次都可以计算elapsedTime用户返回时计活动:

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!

其设置为计时表的基础不起作用。可以someon解释究竟是什么基地的意思,或如何做到这一点?

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

非常感谢! BYE

推荐答案

您可以使用天文台表

您也应该检查此线程

编辑:解决方法:

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();
     }
 };
}

这篇关于安卓:天文台作为一个持续的秒表。如何设置启动时间?什么是天文台表"基地"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆