首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在倒计时计时器结束后,是否将用户发送到新活动?

在倒计时计时器结束后,是否将用户发送到新活动?
EN

Stack Overflow用户
提问于 2018-06-15 13:16:22
回答 5查看 1.2K关注 0票数 0

我想发送用户AbcActivity到XyzActivity后,倒计时计时器是finished.like这个我在AbcActivity上设置了5分钟的倒计时计时器,当倒计时计时器将是0或完成时,它会自动发送用户AbcActivity到XyzActivity。

我该怎么做呢?

`

代码语言:javascript
复制
    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);

}

}

`

EN

回答 5

Stack Overflow用户

发布于 2018-06-15 13:41:40

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

票数 0
EN

Stack Overflow用户

发布于 2018-06-15 13:46:53

代码语言:javascript
复制
    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();
票数 0
EN

Stack Overflow用户

发布于 2018-06-15 13:55:47

代码语言:javascript
复制
    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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50869478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档