创建简单的计时器android

时间:2013-07-17 16:00:34

标签: java android timer

我想制作一个简单的计时器来计算用户在我的应用中执行某些操作所需的时间。我的想法是简单地启动计时器,停止计时器然后显示时间。我一直在寻找并且还没有找到一个可行的确定解决方案。它是我想象的那么简单,还是像我从搜索中发现的那样复杂?

3 个答案:

答案 0 :(得分:2)

计时器需要多精确?

最简单的方法是在执行任务之前花些时间并在执行任务后从时间中减去它:

long start = System.currentTimeMillis();
// do some task
long timeTakenMs = System.currentTimeMillis() - start;

如果您的意思是用户事件驱动的计时器,您可以应用与上述相同的原则:

// Declare instance variable
long start = 0L;

// OnStartTimer
start = System.currentTimeMillis();

// OnStopTimer
long elapsed = System.currentTimeMillis() - start;

答案 1 :(得分:0)

你可以改编这个例子:

<string name="app_name">AndroidTimerExample</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="timerVal">00:00:00</string>
<string name="pauseButtonLabel">Pause</string>
<string name="startButtonLabel">Start</string>
 </resources>


  import android.app.Activity;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.SystemClock;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;

  public class MainActivity extends Activity {

private Button startButton;
private Button pauseButton;

private TextView timerValue;

private long startTime = 0L;

private Handler customHandler = new Handler();

long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    timerValue = (TextView) findViewById(R.id.timerValue);

    startButton = (Button) findViewById(R.id.startButton);

    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

        }
    });

    pauseButton = (Button) findViewById(R.id.pauseButton);

    pauseButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);

        }
    });

}

private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        timerValue.setText("" + mins + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }

};

}

答案 2 :(得分:0)

这是一个主要基于this link的静态类,部分来自@Manolescu的回答。我不知道它是傻还是好,但它在两个应用程序中对我有用:

import android.app.Activity;
import android.os.Handler;
import android.os.SystemClock;

public class Timer extends Activity {
    public static Handler customHandler     = new Handler();
    public static long startTime           ;
    public static long timeInMilliseconds  ;
    public static long timeSwapBuff        ;
    public static long updatedTime         ;
    public static String timerValue        ;

    Timer(){
        initTimeParams();
    }
    public static void startTimer(){
        startTime = SystemClock.uptimeMillis();
        customHandler.postDelayed(updateTimerThread, 0) ;
    }
    public static void pauseTimer(){
        timeSwapBuff += timeInMilliseconds;
        customHandler.removeCallbacks(updateTimerThread);
    }
    public static void resetTimer(){
        initTimeParams();
        displayTimer();
        customHandler = new Handler();
        pauseTimer();
    }
    public static Runnable updateTimerThread = new Runnable() {
        public void run() {
            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
            updatedTime = timeSwapBuff + timeInMilliseconds;
            displayTimer();
            customHandler.postDelayed(this, 0) ;
        }
    };
    public static String displayTimer(){
        int secs = (int) (updatedTime / 1000) ;
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000) ;
        timerValue = ("" + mins + ":"
                + String.format("%02d", secs) + "."
                + String.format("%03d", milliseconds));
        return timerValue;
    }
    public static void initTimeParams(){
        startTime = 0L ;
        timeInMilliseconds = 0L;
        timeSwapBuff = 0L;
        updatedTime = 0L;
        timerValue = "00:00.000";
    }
}

以下是我在(相当蹩脚)的测试应用程序中使用它的方法,但它也在Deitel的Android Programming Flag应用程序中完成了它的工作(总用户时间不包括显示'正确'的时间)。 (即,我开始,暂停,重新启动,并最终重置为下一个'测验'(未显示运行计时器):

    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;


    public class MainActivity extends Activity {

        Button btnStart, btnPause, btnReset, btnShowTime;
        TextView time;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnStart = (Button)findViewById(R.id.btnStart);
            btnPause = (Button)findViewById(R.id.btnPause);
            btnReset = (Button)findViewById(R.id.btnReset);
            btnShowTime = (Button)findViewById(R.id.btnShowTime);
            time = (TextView)findViewById(R.id.textView);
        }
        public void start(View view) {
            Timer.startTimer();
        }
        public void pause(View view) {
            Timer.pauseTimer();
            showTime(view);
        }
        public void reset(View view) {
            Timer.resetTimer();
            showTime(view);
        }
        public void showTime(View view) {
            time.setText(Timer.displayTimer());
        }
    }

这是xml:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:id="@+id/btnStart"
        android:onClick="start"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="152dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause"
        android:id="@+id/btnPause"
        android:onClick="pause"
        android:layout_alignBottom="@+id/btnStart"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reset"
        android:id="@+id/btnReset"
        android:onClick="reset"
        android:layout_marginTop="92dp"
        android:layout_below="@+id/btnPause"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show timer"
        android:id="@+id/btnShowTime"
        android:onClick="showTime"
        android:layout_below="@+id/btnReset"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="00:00.000"
        android:id="@+id/textView"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:phoneNumber="true" />
</RelativeLayout>

strings.xml

<resources>
    <string name="app_name">TimerTestWithTimerAsClass</string>

    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
</resources>

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>