2013-07-17 94 views
0

我想做一個簡單的計時器來計算用戶需要多長時間才能在我的應用中執行某些操作。我想到的只是簡單地啓動計時器,停止計時器並顯示時間。我一直在尋找並沒有找到一個可行的解決方案。這是否像我設想的那樣簡單,還是像我從搜索中發現的那樣更復雜?創建簡單的計時器android

回答

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; 
+0

好的,但如何得到它格式正確?這是我試過'time = System.currentTimeMillis() - 秒; SimpleDateFormat fmt = new SimpleDateFormat(「mm:ss」); String dateString = fmt.format(time);'並且不能正確顯示。它顯示18:30和其他時間太高 – ez4nick

+0

@ ez4nick檢出DateUtils API http://developer.android.com/reference/android/text/format/DateUtils.html#formatElapsedTime(long)這些方法是相當不錯的並且容易。 – Samuel

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

}; 

}

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編程標記應用中顯示「正確」的時間)。 (即,我開始,暫停,重新開始,並最終重新設置爲下一個 '測驗'(不顯示正在運行的定時器):

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> 
相關問題