Passing Countdown Timer beetwen Activities in Android

泄露秘密 提交于 2019-12-25 06:51:22

问题


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:


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.



来源:https://stackoverflow.com/questions/27130057/passing-countdown-timer-beetwen-activities-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!