Txispas
Txispas

Reputation: 159

Capture the countdown timer in different activities

I´am designing a countdown timer, that works perfectly but only If I stay in this Activity. Because, if the countdown starts and I go back for example and start other Activity, the countdonws follows counting down. And logically if I start again the activty where the countdown timer is, it appears like nothing is counting down, but in the bacground is counting down, I know because on the ontick of the timer, I throw a continue notification to show it at the notification tab.

The code is bellow, sendnotification method is a method that throws a alert when the time finishes, and the sendnotificationTiempo to send a notification to the tab every second to show at the top.

public static final int NOTIFICATION_ID = 33;
public static final int NOTIFICATION_ID_Tiempo = 34;
private int minCrono;
TextView text;
MyCounter timer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    StartCrono = (Button) findViewById(R.id.butt_start);
    CancelCrono = (Button) findViewById(R.id.butt_cancel);
    text=(TextView)findViewById(R.id.text_countTime1);

    CancelCrono.setEnabled(false);


    minCrono = mins.getCurrentItem();

    StartCrono.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            minCrono=(60000*minCrono);      
            timer = new MyCounter(minCrono,1000);
            timer.start();
        }
    });

    CancelCrono.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            timer.cancel();     
            text.setText("El tiempo se ha cancelado");
            minutosCrono =  mins.getCurrentItem();

            if (mNotificationManager != null)
                mNotificationManager.cancel(NOTIFICATION_ID_Tiempo);

        }           
    });
}

Countdown Class:

public class MyCounter extends CountDownTimer{          
    public MyCounter(long millisInFuture, long countDownInterval) {     
        super(millisInFuture, countDownInterval);           
    }
     @Override
    public void onFinish() {

        StartCrono.setEnabled(true);
        CancelCrono.setEnabled(false);
        mins.setEnabled(true);
        minCrono = mins.getCurrentItem(); 
        // 

        text.setText("Time Complete!"); 

        sendnotification("Guau", "Time finished.");

    }
    @Override
     public void onTick(long millisUntilFinished) {
        long segRestantesTotal=(millisUntilFinished/1000);
        long minRestantes= (segRestantesTotal/60);
        long segRestantes= (segRestantesTotal%60);


        if(segRestantes>=10){
            text.setText(minRestantes+":"+segRestantes);
            sendnotificationTiempo("Guau", minRestantes+":"+segRestantes);
        }
        else{
            text.setText(minRestantes+":0"+segRestantes);
            sendnotificationTiempo("Guau", minRestantes+":0"+segRestantes);
        }
    }   
}   

}

So what I would to do, is to know when I go back the Activity and I start another one, to show the time what is counting down at the background to show it for example in a textview.

Or maybe if I can get the time that is counting down from the notification tab perfect. Thanks!

Upvotes: 3

Views: 1512

Answers (1)

Rich
Rich

Reputation: 36806

Move your MyCounter instance out of the Activity. When the Activity restarts, the instance is being recreated so the old one is gone. There are a couple ways you can do this, but you need to have your MyCounter instance live somewhere that the Activity can access it but isn't responsible for its life cycle. I use a rudimentary ServiceLocator implementation that I wrote, but you can easily create a custom Application class and keep the timer (or a controller class that creates and controls the timer) there.

Upvotes: 0

Related Questions