2013-07-17 2 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"); Строка dateString = fmt.format (время); 'и она не отображается правильно. он показывает 18:30 и другие времена, которые слишком высоки – ez4nick

+0

@ ez4nick Проверьте API DateUtils 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"; 
    } 
} 

Вот как я использовал его в (довольно хромой) тест-приложение, но он также сделал свою работу (общее время пользователя, который исключает время, потраченное на показ «Корректно») в приложении «Android Flag» от Deitel. (Т.е., я начал, сделал паузу, перезапущен, и в конечном итоге восстановить на следующий 'Тест' (работает таймер не показан):

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> 
Смежные вопросы