Passing Countdown Timer beetwen Activities in Andr

2019-09-07 03:01发布

I'm trying to pass the a CountDownTimer value from Activity 1(PrimerNivel) to Activity 2(SegundoNivel)and start the CountDownTimer in Activity 2 from the value that got from the Activity 1.

But the CountDownTimer in Activity 2 is reset to zero. I can't find the error. could someone help me?

This is the code Activity 1:

public class PrimerNivel extends InicioActivity {
    private TextView cuentaRegresiva;
    long startTime = 60 * 1000;
    private final long interval = 1 * 1000;
    MyCountDownTimer countDownTimer;
    long tiempoRestante;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.juego);
    cuentaRegresiva=(TextView)findViewById(R.id.cuentaRegresiva);
    countDownTimer = new MyCountDownTimer(startTime, interval);
    cuentaRegresiva.setText(cuentaRegresiva.getText() + String.valueOf(startTime / 1000));

}

 public class MyCountDownTimer extends CountDownTimer {
              public MyCountDownTimer(long startTime, long interval) {
                  super(startTime, interval);

              }

                    @Override
                    public void onFinish() {
                        cuentaRegresiva.setText("");


                        }

                    @Override
                    public void onTick(long millisUntilFinished) {
                        tiempoRestante= millisUntilFinished;
                        cuentaRegresiva.setText(""+millisUntilFinished/1000);
                        }}
OnClickListener siguiente =new OnClickListener(){
                     public void onClick(View arg0) {

                         Intent aNivelSiguiente = new Intent(PrimerNivel.this, SegundoNivel.class);
                         aNivelSiguiente.putExtra("regresivaAnterior", tiempoRestante);
                        startActivity(aNivelSiguiente);
                        PrimerNivel.this.finish();




                                                      }};

           }

Activity 2:

public class SegundoNivel extends InicioActivity {
    private TextView cuentaRegresiva;
    long startTime;
    private final long interval = 1 * 1000;
    MyCountDownTimer countDownTimer;
    Bundle bundle;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.juego);
    cuentaRegresiva=(TextView)findViewById(R.id.cuentaRegresiva);
    countDownTimer = new MyCountDownTimer(startTime, interval);


    bundle = getIntent().getExtras();
    startTime= bundle.getLong("regresivaAnterior")/1000;
    cuentaRegresiva.setText(""+startTime);
    countDownTimer.start(); 

}

public class MyCountDownTimer extends CountDownTimer {
              public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
              }
                    @Override
                    public void onFinish() {
                        cuentaRegresiva.setText("");        
                        }

                    @Override
                    public void onTick(long millisUntilFinished) {
                        cuentaRegresiva.setText(""+millisUntilFinished/1000);
                                    }}

1条回答
虎瘦雄心在
2楼-- · 2019-09-07 03:50

You are creating the CountDownTimer before getting the value. Simply move the code that creates the timer to below where you get the value.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.juego);
    cuentaRegresiva=(TextView)findViewById(R.id.cuentaRegresiva);

    bundle = getIntent().getExtras();
    startTime= bundle.getLong("regresivaAnterior")/1000;

    // move it here after you've gotten the value
    countDownTimer = new MyCountDownTimer(startTime, interval);
    cuentaRegresiva.setText(""+startTime);
    countDownTimer.start(); 

You may need to cancel your first timer to get the correct value at the correct time. Try calling

countDownTimer.cancel();
countDownTimer = null;

before creating the Intent. Also, you are dividing the startTime by 1000 after you get the value from the Intent. I'm not sure you want to be doing that.

查看更多
登录 后发表回答