倒数计时器结束后,将用户发送到新活动?

时间:2018-06-15 05:16:22

标签: java android android-studio countdown

我希望在倒数计时器结束后将用户AbcActivity发送到XyzActivity。就像这样我在AbcActivity上设置倒数计时器5分钟 倒数计时器将为0或完成它将自动将用户AbcActivity发送到XyzActivity。

我该怎么做?

`

    txtDay = (TextView) findViewById(R.id.txtDay);
    txtHour = (TextView) findViewById(R.id.txtHour);
    txtMinute = (TextView) findViewById(R.id.txtMinute);
    txtSecond = (TextView) findViewById(R.id.txtSecond);
    tvEventStart = (TextView) findViewById(R.id.tveventStart);
    mButton= (Button) findViewById(R.id.button);
   mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent a= new Intent(Start.this,Timetable.class);
            startActivity(a);
        }
    });

    countDownStart();
}
public void countDownStart() {
    handler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, 1000);
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat(
                        "yyyy-MM-dd");
                // Please here set your event date//YYYY-MM-DD
                Date futureDate = dateFormat.parse("2018-11-4");
                Date currentDate = new Date();
                if (!currentDate.after(futureDate)) {
                    long diff = futureDate.getTime()
                            - currentDate.getTime();
                    long days = diff / (24 * 60 * 60 * 1000);
                    diff -= days * (24 * 60 * 60 * 1000);
                    long hours = diff / (60 * 60 * 1000);
                    diff -= hours * (60 * 60 * 1000);
                    long minutes = diff / (60 * 1000);
                    diff -= minutes * (60 * 1000);
                    long seconds = diff / 1000;
                    txtDay.setText("" + String.format("%02d", days));
                    txtHour.setText("" + String.format("%02d", hours));
                    txtMinute.setText(""
                            + String.format("%02d", minutes));
                    txtSecond.setText(""
                            + String.format("%02d", seconds));
                } else {
                    tvEventStart.setVisibility(View.VISIBLE);
                    tvEventStart.setText("The event started!");
                    textViewGone();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    handler.postDelayed(runnable, 1 * 1000);
}

public void textViewGone() {
    findViewById(R.id.LinearLayout1).setVisibility(View.GONE);
    findViewById(R.id.LinearLayout2).setVisibility(View.GONE);
    findViewById(R.id.LinearLayout3).setVisibility(View.GONE);
    findViewById(R.id.LinearLayout4).setVisibility(View.GONE);
    findViewById(R.id.textViewheader1).setVisibility(View.GONE);

}

}

`

5 个答案:

答案 0 :(得分:0)

您可以在android中使用CountDownTimer类来实现此功能。您可以在计时器对象的onFinish()方法中启动xyzActivity

答案 1 :(得分:0)

    int total;
    long interval;
    private int temp;    
// declare before starting CountDownTimer
        total = 3600 * hrs + 60 * min + sec;// outcome will be in seconds
        interval = total * 1000;// outcome will be in milliseconds

CountDownTimer countDownTimer = new CountDownTimer(interval, 1000) {
int hours, minutes, seconds;
                @Override
                public void onTick(long millisUntilFinished) {
                    // when countdown counts
                temp = total - 1;
                hours = temp / 3600;
                temp = temp - 3600 * hours;
                minutes = temp / 60;
                temp = temp - 60 * minutes;
                seconds = temp;
                total = 3600 * hours + 60 * minutes + seconds;

                txtHour.setText("" + String.format("%02d", hours));
                txtMinute.setText(""
                        + String.format("%02d", minutes));
                txtSecond.setText(""
                        + String.format("%02d", seconds));
                }

                @Override
                public void onFinish() {
                    // when the countdown completed intent to another application
                }
            }.start();

答案 2 :(得分:0)

    new CountDownTimer(300000, 1000) {

        public void onTick(long millisUntilFinished) {
            mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            mTextField.setText("done!");
            Intent intent = new Intent(MainActivity.this,NewActivity.class);
            startActivity(intent);
        }
    }.start();

答案 3 :(得分:0)

请更新您的countDownStart menthod

public void countDownStart() {
    CountDownTimer countDownTimer = new CountDownTimer(300000, 1000) {
        @Override
        public void onTick(long l) {
            // every second, this method will be call
        }

        @Override
        public void onFinish() {
            // after 5 minutes, this method will be call
            tvEventStart.setVisibility(View.VISIBLE);
            tvEventStart.setText("The event started!");
            textViewGone();
        }
    };
}

答案 4 :(得分:0)

您可以在此处添加到您的代码中

if (!currentDate.after(futureDate)) {
                long diff = futureDate.getTime()
                        - currentDate.getTime();
                long days = diff / (24 * 60 * 60 * 1000);
                diff -= days * (24 * 60 * 60 * 1000);
                long hours = diff / (60 * 60 * 1000);
                diff -= hours * (60 * 60 * 1000);
                long minutes = diff / (60 * 1000);
                diff -= minutes * (60 * 1000);
                long seconds = diff / 1000;
                txtDay.setText("" + String.format("%02d", days));
                txtHour.setText("" + String.format("%02d", hours));
                txtMinute.setText(""
                        + String.format("%02d", minutes));
                txtSecond.setText(""
                        + String.format("%02d", seconds));

             if (days <= 0) {
                        if (hours <= 0) {
                            if (minutes <= 0) {
                                if (seconds <= 0) {
                                    // put your intent here
                                }
                            }
                        }
                    }


            }