COUNTDOWNTIMER onfinish();取消后仍称呼

时间:2016-04-18 07:49:15

标签: android countdowntimer

我想在点击一个按钮时取消countdountimer的onfinish函数我执行timer.cancel但仍然在调用完成后。

            running = false;
            countDownTimer.cancel();
            countDownTimer = null;  
            finish();
            startActivity(i);

这是倒计时器

private CountDownTimer createTimer() {
    return new CountDownTimer(getIntent().getIntExtra("timer", 5460000),
            1000) {

        @SuppressLint("NewApi")
        public void onTick(long millisUntilFinished) {
            timer.setText("Section time: "
                    + String.format(
                            Locale.ENGLISH,
                            "%d min, %d sec",
                            TimeUnit.MILLISECONDS
                                    .toMinutes(millisUntilFinished),
                            TimeUnit.MILLISECONDS
                                    .toSeconds(millisUntilFinished)
                                    - TimeUnit.MINUTES
                                            .toSeconds(TimeUnit.MILLISECONDS
                                                    .toMinutes(millisUntilFinished))));
            timer_count = (int) millisUntilFinished;
        }

        public void onFinish() {
            if (running) {
                int random;
                if (getIntent().getIntExtra("t_no", 0) < 20) {
                    i = new Intent(getApplicationContext(),
                            Practice_intruction.class);
                    i.putExtra("type", "Test");
                    Random r = new Random();
                    random = r.nextInt(MainActivity.vocab.size() - 11);
                    i.putExtra("q_no", random);
                    i.putExtra("t_no", 20);
                    i.putExtra("answers",
                            getIntent().getStringExtra("answers"));
                    countDownTimer.cancel();
                    countDownTimer = null;
                    running = false;
                    finish();
                    startActivity(i);
                } else if (getIntent().getIntExtra("t_no", 0) < 40) {
                    i = new Intent(getApplicationContext(),
                            Practice_intruction.class);
                    i.putExtra("type", "Test");
                    Random r = new Random();
                    random = r.nextInt(MainActivity.listining.size() - 4);
                    i.putExtra("q_no", random);
                    i.putExtra("t_no", 40);
                    i.putExtra("answers",
                            getIntent().getStringExtra("answers"));
                    countDownTimer.cancel();
                    countDownTimer = null;
                    running = false;
                    finish();
                    startActivity(i);

                } else if (getIntent().getIntExtra("t_no", 0) < 50) {
                    i = new Intent(getApplicationContext(),
                            Practice_intruction.class);
                    i.putExtra("type", "Test");
                    Random r = new Random();
                    random = r.nextInt(MainActivity.reading.size() - 1);
                    i.putExtra("paragraph_id", random + 1);
                    i.putExtra("q_no", 0);
                    i.putExtra("t_no", 50);
                    i.putExtra("answers",
                            getIntent().getStringExtra("answers"));
                    countDownTimer.cancel();
                    countDownTimer = null;
                    running = false;
                    finish();
                    startActivity(i);
                } else if (getIntent().getIntExtra("t_no", 0) < 60) {
                    i = new Intent(getApplicationContext(),
                            Practice_intruction.class);
                    i.putExtra("type", "Test");
                    Random r = new Random();
                    random = r.nextInt(MainActivity.vid.size() - 1);
                    i.putExtra("Scene_ID", random + 1);
                    i.putExtra("q_no", 0);
                    i.putExtra("t_no", 60);
                    i.putExtra("answers",
                            getIntent().getStringExtra("answers"));
                    countDownTimer.cancel();
                    countDownTimer = null;
                    running = false;
                    finish();
                    startActivity(i);
                } else if (getIntent().getIntExtra("t_no", 0) < 100) {
                    i = new Intent(getApplicationContext(),
                            Practice_result.class);
                    i.putExtra("type", getIntent().getStringExtra("type"));
                    i.putExtra("answers",
                            getIntent().getStringExtra("answers"));
                    countDownTimer.cancel();
                    countDownTimer = null;
                    running = false;
                    finish();
                    startActivity(i);
                }
            }

        }

    }.start();
}

这是我活动的一部分,我在使用倒数计时器,我没有在任何地方使用它来这里

else if (getIntent().getStringExtra("type").equalsIgnoreCase("Test")) {

        timer.setVisibility(View.VISIBLE);
        back.setVisibility(View.INVISIBLE);
        running = true;

        countDownTimer = this.createTimer();

        if (getIntent().getIntExtra("t_no", 0) < 10) {

            final ArrayList<HashMap<String, String>> questions = new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < 50; i++) {
                if (MainActivity.structure.get(i).get("type_p_t")
                        .equalsIgnoreCase("t"))
                    questions.add(MainActivity.structure.get(i));
            }
            main_txt.setText("Q"
                    + (getIntent().getIntExtra("t_no", 0) + 1)
                    + ": "
                    + questions.get(getIntent().getIntExtra("q_no", 0))
                            .get("question").replace("#", ""));
            title.setText("Structure/ Sentence Completion");
            opt_a_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[0]);
            opt_b_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[1]);
            opt_c_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[2]);
            opt_d_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[3]);
            cor_ans = questions.get(getIntent().getIntExtra("q_no", 0))
                    .get("cor_ans");
            next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    int random;
                    if (getIntent().getIntExtra("t_no", 0) == 9) {
                        i = new Intent(getApplicationContext(),
                                Practice_intruction.class);
                        i.putExtra("type", "Test");
                        Random r = new Random();
                        random = r.nextInt(MainActivity.structure.size() - 51);
                    } else {
                        i = new Intent(getApplicationContext(),
                                Struct.class);
                        i.putExtra("type",
                                getIntent().getStringExtra("type"));
                        Random r = new Random();
                        random = r.nextInt(MainActivity.structure.size() - 51);
                        while (Helper.sec_questions.contains(random)) {
                            random = r.nextInt(MainActivity.structure
                                    .size() - 51);
                        }
                        Helper.sec_questions.add(random);

                    }
                    i.putExtra("q_no", random);
                    i.putExtra("t_no",
                            getIntent().getIntExtra("t_no", 0) + 1);
                    if (ans.equalsIgnoreCase(cor_ans)) {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "true#");

                    } else {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "false#");
                    }

                    i.putExtra("timer", timer_count);

                    Details_data.answers.add(ans);
                    Details_data.choices.add(opt_a_txt.getText().toString()
                            + "#" + opt_b_txt.getText().toString() + "#"
                            + opt_c_txt.getText().toString() + "#"
                            + opt_d_txt.getText().toString());
                    Details_data.correct_answers.add(cor_ans);
                    Details_data.questions.add(main_txt.getText()
                            .toString());
                    Details_data.why.add(questions
                            .get(getIntent().getIntExtra("q_no", 0))
                            .get("why").replace("#", ""));

                    running = false;
                    countDownTimer.cancel();
                    countDownTimer.onFinish();
                    countDownTimer = null;

                    finish();
                    startActivity(i);
                }
            });

        } else if (getIntent().getIntExtra("t_no", 0) < 20) {

            final ArrayList<HashMap<String, String>> questions = new ArrayList<HashMap<String, String>>();
            for (int i = 50; i < MainActivity.structure.size(); i++) {
                if (MainActivity.structure.get(i).get("type_p_t")
                        .equalsIgnoreCase("t"))
                    questions.add(MainActivity.structure.get(i));
            }
            main_txt.setText("Q"
                    + (getIntent().getIntExtra("t_no", 0) + 1)
                    + ": "
                    + questions.get(getIntent().getIntExtra("q_no", 0))
                            .get("question").replace("#", ""));
            title.setText("Structure/ Error Identification");
            opt_a_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[0]);
            opt_b_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[1]);
            opt_c_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[2]);
            opt_d_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[3]);
            cor_ans = questions.get(getIntent().getIntExtra("q_no", 0))
                    .get("cor_ans");
            next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    int random;
                    if (getIntent().getIntExtra("t_no", 0) == 19) {
                        i = new Intent(getApplicationContext(),
                                Practice_intruction.class);
                        i.putExtra("type", "Test");
                        Random r = new Random();
                        random = r.nextInt(MainActivity.vocab.size() - 11);
                    } else {
                        i = new Intent(getApplicationContext(),
                                Struct.class);
                        i.putExtra("type",
                                getIntent().getStringExtra("type"));
                        Random r = new Random();
                        random = r.nextInt(MainActivity.structure.size() - 51);
                        while (Helper.sec_questions.contains(random)) {
                            random = r.nextInt(MainActivity.structure
                                    .size() - 51);
                        }
                        Helper.sec_questions.add(random);
                    }
                    i.putExtra("q_no", random);
                    i.putExtra("t_no",
                            getIntent().getIntExtra("t_no", 0) + 1);
                    if (ans.equalsIgnoreCase(cor_ans)) {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "true#");

                    } else {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "false#");
                    }

                    i.putExtra("timer", timer_count);

                    Details_data.answers.add(ans);
                    Details_data.choices.add(opt_a_txt.getText().toString()
                            + "#" + opt_b_txt.getText().toString() + "#"
                            + opt_c_txt.getText().toString() + "#"
                            + opt_d_txt.getText().toString());
                    Details_data.correct_answers.add(cor_ans);
                    Details_data.questions.add(main_txt.getText()
                            .toString());
                    Details_data.why.add(questions
                            .get(getIntent().getIntExtra("q_no", 0))
                            .get("why").replace("#", ""));

                    running = false;
                    countDownTimer.cancel();
                    countDownTimer.onFinish();
                    countDownTimer = null;

                    finish();
                    startActivity(i);
                }
            });

        } else if (getIntent().getIntExtra("t_no", 0) < 40) {

            final ArrayList<HashMap<String, String>> questions = new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < MainActivity.vocab.size(); i++) {
                if (MainActivity.vocab.get(i).get("type_p_t")
                        .equalsIgnoreCase("t"))
                    questions.add(MainActivity.vocab.get(i));
            }
            main_txt.setText("Q"
                    + (getIntent().getIntExtra("t_no", 0) + 1)
                    + ": "
                    + questions.get(getIntent().getIntExtra("q_no", 0))
                            .get("question").replace("#", ""));
            title.setText("Vocabulary");
            opt_a_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[0]);
            opt_b_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[1]);
            opt_c_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[2]);
            opt_d_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[3]);
            cor_ans = questions.get(getIntent().getIntExtra("q_no", 0))
                    .get("cor_ans");
            next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    int random;
                    if (getIntent().getIntExtra("t_no", 0) == 39) {
                        i = new Intent(getApplicationContext(),
                                Practice_intruction.class);
                        i.putExtra("type", "Test");
                        Random r = new Random();
                        random = r.nextInt(MainActivity.listining.size() - 4);
                    } else {
                        i = new Intent(getApplicationContext(),
                                Struct.class);
                        i.putExtra("type",
                                getIntent().getStringExtra("type"));
                        Random r = new Random();
                        random = r.nextInt(MainActivity.vocab.size() - 11);
                        while (Helper.sec_questions.contains(random)) {
                            random = r.nextInt(MainActivity.vocab.size() - 11);
                        }
                        Helper.sec_questions.add(random);
                    }
                    i.putExtra("q_no", random);
                    i.putExtra("t_no",
                            getIntent().getIntExtra("t_no", 0) + 1);
                    if (ans.equalsIgnoreCase(cor_ans)) {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "true#");

                    } else {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "false#");
                    }

                    i.putExtra("timer", timer_count);

                    Details_data.answers.add(ans);
                    Details_data.choices.add(opt_a_txt.getText().toString()
                            + "#" + opt_b_txt.getText().toString() + "#"
                            + opt_c_txt.getText().toString() + "#"
                            + opt_d_txt.getText().toString());
                    Details_data.correct_answers.add(cor_ans);
                    Details_data.questions.add(main_txt.getText()
                            .toString());

                    running = false;
                    countDownTimer.cancel();
                    countDownTimer.onFinish();
                    countDownTimer = null;

                    finish();
                    startActivity(i);
                }
            });
        } else if (getIntent().getIntExtra("t_no", 0) < 50) {
            final ArrayList<HashMap<String, String>> questions = new ArrayList<HashMap<String, String>>();
            main_txt.setVisibility(View.GONE);
            opt_a.setVisibility(View.GONE);
            opt_b.setVisibility(View.GONE);
            opt_c.setVisibility(View.GONE);
            opt_d.setVisibility(View.GONE);
            headset.setVisibility(View.VISIBLE);
            for (int i = 0; i < MainActivity.listining.size(); i++) {
                if (MainActivity.listining.get(i).get("type_p_t")
                        .equalsIgnoreCase("t"))
                    questions.add(MainActivity.listining.get(i));
            }
            main_txt.setText("Q"
                    + (getIntent().getIntExtra("t_no", 0) + 1)
                    + ": "
                    + questions.get(getIntent().getIntExtra("q_no", 0))
                            .get("question").replace("#", ""));
            title.setText("Listening Comprehension");
            opt_a_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[0]);
            opt_b_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[1]);
            opt_c_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[2]);
            opt_d_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[3]);
            cor_ans = questions.get(getIntent().getIntExtra("q_no", 0))
                    .get("cor_ans");
            next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    int random;
                    if (getIntent().getIntExtra("t_no", 0) == 49) {
                        i = new Intent(getApplicationContext(),
                                Practice_intruction.class);
                        i.putExtra("type",
                                getIntent().getStringExtra("type"));
                        Random r = new Random();
                        random = r.nextInt(MainActivity.reading.size() - 1);
                        i.putExtra("paragraph_id", random + 1);
                        i.putExtra("q_no", 0);
                    } else {
                        i = new Intent(getApplicationContext(),
                                Struct.class);
                        i.putExtra("type",
                                getIntent().getStringExtra("type"));
                        Random r = new Random();
                        random = r.nextInt(MainActivity.listining.size() - 4);
                        while (Helper.sec_questions.contains(random)) {
                            random = r.nextInt(MainActivity.listining
                                    .size() - 4);
                        }
                        Helper.sec_questions.add(random);
                        i.putExtra("q_no", random);
                    }
                    i.putExtra("t_no",
                            getIntent().getIntExtra("t_no", 0) + 1);
                    if (ans.equalsIgnoreCase(cor_ans)) {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "true#");

                    } else {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "false#");
                    }

                    i.putExtra("timer", timer_count);

                    Details_data.answers.add(ans);
                    Details_data.choices.add(opt_a_txt.getText().toString()
                            + "#" + opt_b_txt.getText().toString() + "#"
                            + opt_c_txt.getText().toString() + "#"
                            + opt_d_txt.getText().toString());
                    Details_data.correct_answers.add(cor_ans);
                    Details_data.questions.add(main_txt.getText()
                            .toString());

                    running = false;
                    countDownTimer.cancel();
                    countDownTimer.onFinish();
                    countDownTimer = null;

                    finish();
                    startActivity(i);
                }
            });

            listen.setVisibility(View.VISIBLE);
            mp = new MediaPlayer();
            listen.setOnClickListener(new OnClickListener() {

                @SuppressLint("SdCardPath")
                @Override
                public void onClick(View v) {
                    try {
                        mp.setDataSource("/mnt/sdcard/qualification/"
                                + questions.get(
                                        getIntent().getIntExtra("q_no", 0))
                                        .get("voice") + ".mp3");
                        mp.prepare();
                        mp.start();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    listen.setVisibility(View.GONE);

                }
            });

            mp.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.release();
                    mp = null;
                    main_txt.setVisibility(View.VISIBLE);
                    opt_a.setVisibility(View.VISIBLE);
                    opt_b.setVisibility(View.VISIBLE);
                    opt_c.setVisibility(View.VISIBLE);
                    opt_d.setVisibility(View.VISIBLE);
                    headset.setVisibility(View.GONE);
                }
            });
        } else if (getIntent().getIntExtra("t_no", 0) < 60) {
            paragraph.setVisibility(View.VISIBLE);
            final ArrayList<HashMap<String, String>> questions = new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < MainActivity.reading_qst.size(); i++) {
                if (MainActivity.reading_qst
                        .get(i)
                        .get("paragraph_id")
                        .equalsIgnoreCase(
                                String.valueOf(getIntent().getIntExtra(
                                        "paragraph_id", 0) + 1)))
                    questions.add(MainActivity.reading_qst.get(i));
            }
            main_txt.setText("Q"
                    + (getIntent().getIntExtra("t_no", 0) + 1)
                    + ": "
                    + questions.get(getIntent().getIntExtra("q_no", 0))
                            .get("question").replace("#", ""));
            title.setText("Reading Comprehension");
            opt_a_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[0]);
            opt_b_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[1]);
            opt_c_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[2]);
            opt_d_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[3]);
            cor_ans = questions.get(getIntent().getIntExtra("q_no", 0))
                    .get("cor_ans");
            next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    int random;
                    if (getIntent().getIntExtra("t_no", 0) == 59) {
                        i = new Intent(getApplicationContext(),
                                Practice_intruction.class);
                        i.putExtra("type", "Test");
                        Random r = new Random();
                        random = r.nextInt(MainActivity.vid.size() - 1);
                        i.putExtra("Scene_ID", random + 1);
                        i.putExtra("q_no", 0);
                    } else if (getIntent().getIntExtra("t_no", 0) == 54) {
                        i = new Intent(getApplicationContext(),
                                Reading.class);
                        i.putExtra("type", "Test");
                        Random r = new Random();
                        random = r.nextInt(MainActivity.reading.size() - 1);
                        while (Helper.sec_questions.contains(random)) {
                            random = r.nextInt(MainActivity.reading.size() - 2);
                        }
                        Helper.sec_questions.add(random + 1);
                        i.putExtra("paragraph_id", random + 1);
                        i.putExtra("q_no", 0);
                    } else {
                        i = new Intent(getApplicationContext(),
                                Struct.class);
                        i.putExtra("type",
                                getIntent().getStringExtra("type"));
                        i.putExtra("paragraph_id",
                                getIntent().getIntExtra("paragraph_id", 0));
                        i.putExtra("q_no",
                                getIntent().getIntExtra("q_no", 0) + 1);
                    }
                    i.putExtra("t_no",
                            getIntent().getIntExtra("t_no", 0) + 1);
                    if (ans.equalsIgnoreCase(cor_ans)) {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "true#");
                    } else {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "false#");
                    }
                    i.putExtra("timer", timer_count);

                    Details_data.answers.add(ans);
                    Details_data.choices.add(opt_a_txt.getText().toString()
                            + "#" + opt_b_txt.getText().toString() + "#"
                            + opt_c_txt.getText().toString() + "#"
                            + opt_d_txt.getText().toString());
                    Details_data.correct_answers.add(cor_ans);
                    Details_data.questions.add(main_txt.getText()
                            .toString());

                    running = false;
                    countDownTimer.cancel();
                    countDownTimer.onFinish();
                    countDownTimer = null;

                    finish();
                    startActivity(i);
                }
            });
        }

        else if (getIntent().getIntExtra("t_no", 0) < 100) {
            final ArrayList<HashMap<String, String>> questions = new ArrayList<HashMap<String, String>>();
            for (int i = 0; i < MainActivity.vid_qst.size(); i++) {
                if (MainActivity.vid_qst
                        .get(i)
                        .get("Scene_ID")
                        .equalsIgnoreCase(
                                String.valueOf(getIntent().getIntExtra(
                                        "Scene_ID", 0) + 1)))
                    questions.add(MainActivity.vid_qst.get(i));
            }
            main_txt.setText("Q"
                    + (getIntent().getIntExtra("t_no", 0) + 1)
                    + ": "
                    + questions.get(getIntent().getIntExtra("q_no", 0))
                            .get("question").replace("#", ""));
            title.setText("Visual Comprehension");
            opt_a_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[0]);
            opt_b_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[1]);
            opt_c_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[2]);
            opt_d_txt.setText(questions
                    .get(getIntent().getIntExtra("q_no", 0)).get("choices")
                    .split("#")[3]);
            cor_ans = questions.get(getIntent().getIntExtra("q_no", 0))
                    .get("cor_ans");
            next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (getIntent().getIntExtra("t_no", 0) == 99) {
                        i = new Intent(getApplicationContext(),
                                Practice_result.class);
                        i.putExtra("type",
                                getIntent().getStringExtra("type"));
                    } else if (getIntent().getIntExtra("t_no", 0) == 59
                            || getIntent().getIntExtra("t_no", 0) == 69
                            || getIntent().getIntExtra("t_no", 0) == 79
                            || getIntent().getIntExtra("t_no", 0) == 89) {
                        i = new Intent(getApplicationContext(), Video.class);
                        i.putExtra("type", "Test");
                        Random r = new Random();
                        int random = r.nextInt(MainActivity.vid.size() - 1);
                        while (Helper.sec_questions.contains(random)) {
                            random = r.nextInt(MainActivity.vid.size() - 1);
                        }
                        Helper.sec_questions.add(random + 1);
                        i.putExtra("Scene_ID", random + 1);
                        i.putExtra("q_no", 0);
                    } else {
                        i = new Intent(getApplicationContext(),
                                Struct.class);
                        i.putExtra("type",
                                getIntent().getStringExtra("type"));
                        i.putExtra("q_no",
                                getIntent().getIntExtra("q_no", 0) + 1);
                        i.putExtra("Scene_ID",
                                getIntent().getIntExtra("Scene_ID", 0));
                    }
                    i.putExtra("t_no",
                            getIntent().getIntExtra("t_no", 0) + 1);
                    if (ans.equalsIgnoreCase(cor_ans)) {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "true#");
                    } else {
                        i.putExtra("answers",
                                getIntent().getStringExtra("answers")
                                        + "false#");
                    }
                    i.putExtra("timer", timer_count);

                    Details_data.answers.add(ans);
                    Details_data.choices.add(opt_a_txt.getText().toString()
                            + "#" + opt_b_txt.getText().toString() + "#"
                            + opt_c_txt.getText().toString() + "#"
                            + opt_d_txt.getText().toString());
                    Details_data.correct_answers.add(cor_ans);
                    Details_data.questions.add(main_txt.getText()
                            .toString());

                    running = false;
                    countDownTimer.cancel();
                    countDownTimer.onFinish();
                    countDownTimer = null;

                    finish();
                    startActivity(i);
                }
            });
        }

    }

    opt_a.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            opt_a_img.setImageResource(R.drawable.ans_sel_yallow);
            opt_b_img.setImageResource(R.drawable.ans_sel_white);
            opt_c_img.setImageResource(R.drawable.ans_sel_white);
            opt_d_img.setImageResource(R.drawable.ans_sel_white);

            ans = "a";

            next.setVisibility(View.VISIBLE);
            paragraph.setVisibility(View.GONE);

        }
    });

    opt_b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            opt_a_img.setImageResource(R.drawable.ans_sel_white);
            opt_b_img.setImageResource(R.drawable.ans_sel_yallow);
            opt_c_img.setImageResource(R.drawable.ans_sel_white);
            opt_d_img.setImageResource(R.drawable.ans_sel_white);

            ans = "b";

            next.setVisibility(View.VISIBLE);
            paragraph.setVisibility(View.GONE);
        }
    });

    opt_c.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            opt_a_img.setImageResource(R.drawable.ans_sel_white);
            opt_b_img.setImageResource(R.drawable.ans_sel_white);
            opt_c_img.setImageResource(R.drawable.ans_sel_yallow);
            opt_d_img.setImageResource(R.drawable.ans_sel_white);

            ans = "c";

            next.setVisibility(View.VISIBLE);
            paragraph.setVisibility(View.GONE);
        }
    });

    opt_d.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            opt_a_img.setImageResource(R.drawable.ans_sel_white);
            opt_b_img.setImageResource(R.drawable.ans_sel_white);
            opt_c_img.setImageResource(R.drawable.ans_sel_white);
            opt_d_img.setImageResource(R.drawable.ans_sel_yallow);

            ans = "d";

            next.setVisibility(View.VISIBLE);
            paragraph.setVisibility(View.GONE);
        }
    });

    back.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            AlertDialog alert = new AlertDialog.Builder(Struct.this)
                    .create();

            if (alert != null) {
                alert.setMessage("Are you sure you want to exit?!");
                alert.setCancelable(true);
                alert.setButton("Yes",
                        new DialogInterface.OnClickListener() {
                            @SuppressLint("NewApi")
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                Intent i = new Intent(
                                        getApplicationContext(),
                                        MainMenu.class);
                                running = false;
                                if (countDownTimer != null) {
                                    countDownTimer.cancel();
                                    countDownTimer.onFinish();
                                    countDownTimer = null;
                                }

                                finish();
                                startActivity(i);
                            }
                        });
                alert.setButton2("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {

                            }
                        });
                alert.setCanceledOnTouchOutside(false);

                alert.show();
                TextView msg = (TextView) alert
                        .findViewById(android.R.id.message);
                if (msg != null)
                    msg.setTextSize(20);

                Button b1 = (Button) alert
                        .findViewById(android.R.id.button1);
                if (b1 != null)
                    b1.setTextSize(20);

                Button b2 = (Button) alert
                        .findViewById(android.R.id.button2);
                if (b2 != null)
                    b2.setTextSize(20);

            }

        }
    });

    paragraph.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            i = new Intent(getApplicationContext(), Reading.class);
            i.putExtra("type", getIntent().getStringExtra("type"));
            i.putExtra("paragraph_id",
                    getIntent().getIntExtra("paragraph_id", 0));
            i.putExtra("q_no", getIntent().getIntExtra("q_no", 0));
            i.putExtra("t_no", getIntent().getIntExtra("t_no", 0));
            i.putExtra("answers", getIntent().getStringExtra("answers"));
            i.putExtra("timer", timer_count);
            i.putExtra("return", true);

            startActivity(i);
        }
    });
}

3 个答案:

答案 0 :(得分:1)

似乎在任何情况下都会调用finish()。但是,如果在取消之前将running设置为false,则finish()方法不执行任何操作,因此它应该没问题。

running = false;
countdownTimer.cancel();

答案 1 :(得分:0)

请注意,在代码段中,您尝试在onFinish()方法中取消计时器。这不是必需的,也不会对计时器产生任何影响。

此外,在Log.v("SomeTag", "onFinish() called");之上拨打onFinish(),在Log.v("SomeTag", "OnClick() called");之上拨打onClick(),然后检查logcat输出。您是否绝对确定onFinish()onClick()实际上已被调用,而您所看到的并不是其他代码的副作用?请注意,finish();不会立即完成活动,而是仅在执行当前方法后返回。

答案 2 :(得分:0)

我已经基于@Ridcully回答做了自己的CountDownTimer:

public abstract class CustomCountDownTimer extends CountDownTimer {

public boolean running;

public CustomCountDownTimer(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    running = true;
}

@Override
public void onTick(long millisUntilFinished) {
    tick(millisUntilFinished);
}

@Override
public void onFinish() {
    if(running) {
        finish();
    }
}

public abstract void finish();
public abstract void tick(long millisUntilFinished);

public void stop(){
    running = false;
    cancel();
}

}

您现在应该调用stop()而不是cancel()