如何在计时器倒数结束后调用其他活动

时间:2016-05-01 11:56:09

标签: android timer

这是我的计时器类,我想在计时器为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于2016年5月1日创建。  * / 公共课Playagin扩展活动{

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


}

1 个答案:

答案 0 :(得分:1)

在TimerCountDown类中创建私有成员Context。当您在计时器中创建计时器传递上下文(在大多数情况下,当前活动通过"此")时。在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;
    }
}