无法获取活动的上下文扩展CountDownTimer

时间:2018-03-13 00:54:09

标签: java android countdowntimer

我想将此活动中的变量转移到另一个变量,但我发现无法获取活动的上下文。

我已经尝试了每一个选项,我一直在通过StackOverflow进行搜索,看起来人们使用getContextApplication(),但它对我不起作用。

有什么想法吗?

public class CountDownCalculator2 extends CountDownTimer {

    private String countDown;

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

    @Override
    public void onTick(long millisUntilFinished) {

        long day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
        millisUntilFinished -= TimeUnit.DAYS.toMillis(day);

        long hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
        millisUntilFinished -= TimeUnit.HOURS.toMillis(hour);

        long minute = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
        millisUntilFinished -= TimeUnit.MINUTES.toMillis(minute);

        long second = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);

        this.countDown = String.format("%02d:%02d:%02d:%02d", day, hour, minute, second);

        Intent intent = new Intent(this, VocabularyAdapter.class);
        intent.putExtra("timer", countDown);
    }

    @Override
    public void onFinish() { 
        countDown = "In Cinemas";
    }
}

1 个答案:

答案 0 :(得分:0)

要使用活动上下文,您需要传递它

private Context context;

public CountDownCalculator2(Context context, long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    this.context = context;
}