2012-02-21 9 views
1

ユーザーの再生時間を追跡するために使用できる、AndroidのViewのサブクラスであるタイマー機能またはタイマーはありますか?アンドロイドにタイマー機能はありますか? (java.util.Timerではない)

ビルドする必要がある場合、1000ms(Thread.sleep(1000))のスリープ状態になるループを持つThreadまたはRunnableクラスを作成し、それ自体を更新するのがよいでしょうか。例えば

public class TimerThread extends Runnable{ 

    private int time; 
    boolean run = true; 

    public void run(){ 
     while(run){ 
      //update the View here 
      Thread.sleep(1000); 
      time++; 
     } 
    } 
} 
+0

これは常に変化して示すか、ゲームが終了した後だけ示しますか? – Maurice

+0

掃海艇ゲームのタイマーのように常に変更する – Marl

答えて

1

これは参考になるかもしれません。

http://developer.android.com/reference/android/widget/Chronometer.html

私はあなたが本当にそれを自分自身を使用していないが、私はそれを聞いたん例えばGoogleに必要かもしれないと思います。ただ、ここでは、より多くのオプションを提供するために、

+0

「カウントアップ」タイマーが必要な場合は、 。 – Maurice

1

私のゲームで使用しているコードです。役に立ったら嬉しいです。

MyCount counter = new MyCount(30000,10); //set to 30 seconds 
counter.start(); 

public class MyCount extends CountDownTimer{ 
    long time=0; 
    public MyCount(long millisInFuture, long countDownInterval) { 
    super(millisInFuture, countDownInterval); 
    } 
    @Override 
    public void onFinish() { 
    clock.setText("You lose!"); //clock is a textfield in the game 
    restart(); //call you own restart method 
    } 
    @Override 
    public void onTick(long millisUntilFinished) { 
    DecimalFormat df=new DecimalFormat("#.##"); //import java.text.DecimalFormat; 
    clock.setText("Left: "+ df.format(millisUntilFinished/1000.0)); 
    time=millisUntilFinished; 
    } 
    public long getTime(){ 
     return time; 
    } 
    } 
関連する問題