同时在两个不同的活动上显示两个倒计时器

时间:2015-12-16 20:04:46

标签: java android timer

我目前在第二项活动中有一个倒数计时器。我想要做的是将我的主要活动上的textView设置为我的secondActivity上的倒数计时器的值。这是倒数计时器的代码。

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() {

    }


}}

1 个答案:

答案 0 :(得分:1)

1 - 在ServiceIntentService

中运行您的计时器

2-在每个tick上发布来自服务的事件(消息)。  你可以做到这一点 使用Mered - reportLocalBroadcastManager。将事件发送到Service请求立即更新也是一件好事。

就个人而言,我会使用Otto作为它使用起来更方便。

编辑: 我的意思是。将您的计时器与Activity

放在一起,使其与Service分开

然后在您的onTick()方法

启动新的Otto事件并通过示例传递当前计数器时间:

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

您可以在Event

中收听此Activity
@Subscribe public void eventTimerTick(TimerTickEvent event) {
    // TODO: React to the event somehow!

      event.getTimeToEnd();
}

我强烈建议您阅读Otto之前使用它之前需要注册活动简历并取消注册活动暂停