0xOsiris
0xOsiris

Reputation: 277

Simultaneously displaying two countdown timers on two different activities

I currently have a countdown timer on my second activity. What I am trying to do is set the textView on my main activity to the value of the countdown timer on my secondActivity. Here is my code for the countdown timer.

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // put your logic here to set the time with hourPicked and minPicked variables
                timer = new CounterClass((hours * 60 * 1000) + (minutes * 1000), 1000);
                String hms = String.format(("%02d:%02d:"), hours, minutes);
                textViewTime.setText(hms);
                timer.start();
            }
        });

        noPickerHours.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                hours = newVal;
                timer = new CounterClass((hours * 60 * 1000), 1000);
                String hms = String.format(("%02d:%02d"), hours, minutes);
                textViewTime.setText(hms);
            }
        });

        noPickerMinutes.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                minutes = newVal;
                timer = new CounterClass((minutes * 1000), 1000);
                String hms = String.format(("%02d:%02d"), hours, minutes);
                textViewTime.setText(hms);
                Bundle timerBundle = new Bundle();
                timerBundle.putInt("%02d:%02d", Integer.parseInt(hms));
                Intent timerIntent = new Intent();


            }
        })
public class CounterClass extends CountDownTimer {


    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */

    public CounterClass(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }



    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override

    public void onTick(long millisUntilFinished) {
        long millis = millisUntilFinished;
        String hms = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
        System.out.println(hms);
        textViewTime.setText(hms);


    }

    @Override
    public void onFinish() {

    }


}}

Upvotes: 0

Views: 331

Answers (1)

Adam Fręśko
Adam Fręśko

Reputation: 1064

1 - Run your timer in Service or IntentService

2- Post events(messages) from service on each tick. You can accomplish that using LocalBroadcastManager or Otto. It would be good to also send event to Service requesting immediate update.

Personally i would use Otto as its so much more convenient to use.

Edit: What i mean is. Make your timer separated from Activity by placing it inside Service

Then in your onTick() method

start new Otto event and pass current counter time with it example:

public void onTick(long millisUntilFinished) {
          bus.post(new TimerTickEvent(millisUntilFinished));
}

You can listen for this Event in your Activity

@Subscribe public void eventTimerTick(TimerTickEvent event) {
    // TODO: React to the event somehow!

      event.getTimeToEnd();
}

I higly advice you to read about Otto Before using it as it requires registering on activity resum and unregistering on activity pause

Upvotes: 1

Related Questions