2016-05-01 23 views
-1

これは私のタイマークラスです。タイマーがenbのときに他のアクティビティを呼び出すことができます。 は、ここにコード他のアクティビティをタイマーカウントダウン終了後に呼び出す方法

import android.content.Intent; 
import android.os.CountDownTimer; 
import android.widget.TextView; 


public class TimerCountDown extends CountDownTimer { 





    private TextView timerTextView; 






    public TimerCountDown(long startTime, long interval) { 
     super(startTime, interval); 





    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     getTimerTextView().setText((String.valueOf(millisUntilFinished/1000 + "S"))); 




    } 

    @Override 
    public void onFinish() { 
     getTimerTextView().setText("0s"); 


     //need to start activity 

    } 





    public TextView getTimerTextView() { 
     return timerTextView; 
    } 

    public void setTimerTextView(TextView timerTextView) { 
     this.timerTextView = timerTextView; 
    } 



} 

と、この私は何

import android.app.Activity; 

輸入をandroid.os.Bundle表示する活性を入力してください。

/** *作成者:eli投稿日:5/1/2016 Playaginアクティビティ{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.play_again); 


} 

答えて

1

があなたのTimerCountDownクラスのプライベートメンバコンテキストを作る拡張する*/publicクラス 。タイマーでContextを渡す(ほとんどの場合、現在のアクティビティ "this"経由で)タイマーを作成します。 onFinishメソッドでは、コンテキストを使用してPlayaginアクティビティを開始します。

タイマークラスは次のようになります。

public class TimerCountDown extends CountDownTimer { 

    private TextView timerTextView; 
    private Context context; 

    public TimerCountDown(long startTime, long interval, Context context) { 
     super(startTime, interval); 
     this.context = context; 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     getTimerTextView().setText((String.valueOf(millisUntilFinished/1000 + "S"))); 
    } 

    @Override 
    public void onFinish() { 
     getTimerTextView().setText("0s"); 

     context.startActivity(new Intent(context, Playagin.class)); 
    } 

    public TextView getTimerTextView() { 
     return timerTextView; 
    } 

    public void setTimerTextView(TextView timerTextView) { 
     this.timerTextView = timerTextView; 
    } 
} 
+0

ありがとう、それは問題を解決しました – Zcode

関連する問題