如何在特定时间段后计划活动?

时间:2014-07-01 20:00:03

标签: android android-activity time

我想创建一个应用程序,其中用户有90秒才能完成一定数量的总和。

我不确定如何在时间框架结束后停止活动并转移到另一个活动?

活动代码:

/**
 * Class holding the activity that has the 10 random sums for the user to answer
 * @author Ross
 * 
 */
public class RandomTest extends Activity implements View.OnClickListener {
    // declare vars
    TextView text;
    EditText answer;
    Button submit;
    int random1;
    int random2;
    String[] question = new String[10];
    int correctAnswer[] = new int[10];
    int[] results = new int[10];
    int score = 0;
    int questionNumber = 1;
    MediaPlayer correctNoise;
    MediaPlayer incorrectNoise;
    ImageView imageRandom;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test);

        // initialising variables
        initialiseVars();

        // set up random
        setUpRandom();

        // Set text view equal to question in array
        text.setText(question[questionNumber - 1]);

        // set on click listener for the submit button
        submit.setOnClickListener(this);

        // updateQuestion
        updateQuestion();

    }

    /**
     * Method that initialises variables
     */
    public void initialiseVars() {

        correctNoise = MediaPlayer.create(RandomTest.this, R.raw.correctnoise);
        incorrectNoise = MediaPlayer.create(RandomTest.this, R.raw.incorrectnoise);
        text = (TextView) findViewById(R.id.tvTopRandomTest);
        answer = (EditText) findViewById(R.id.etEnterAnswerRandomTest);
        submit = (Button) findViewById(R.id.btnSubmitRandomTest);
        imageRandom= (ImageView) findViewById(R.id.imageViewRandomTest);

    }

    /**
     * Method that creates the random sum for user to answer
     */
    public void setUpRandom() {

        // setting up new random
        Random random = new Random();

        // Generating random number between 1 and 12
        random1 = random.nextInt(12) + 1;
        // Generating another random number between 1 and 12
        random2 = random.nextInt(12) + 1;
        // Creating random question String
        question[questionNumber - 1] = random1 + " x " + random2 + " = ";
        // Creating correct answer to question
        correctAnswer[questionNumber - 1] = random1 * random2; 

    }

    /**
     * Method that updates question after each click
     */
    public void updateQuestion() {

        // updating question after each click
        setUpRandom();
        text.setText(question[questionNumber - 1]);
        answer.setText("");

    }

    public void onClick(View v) {

        // sets text view equal to what is entered in editText
        final String entry = answer.getText().toString();
        // convert from string value to int
        int a = Integer.parseInt(entry); //

        // setting the user answer equal to the correct part of results array
        results[questionNumber - 1] = a;

        // If user answer is equal to correct answer then increase score
        if (a == correctAnswer[questionNumber - 1]) {
            score++;
            correctNoise.start();
            imageRandom.setImageResource(R.drawable.thumbsup);
        }else{

            incorrectNoise.start();
            imageRandom.setImageResource(R.drawable.thumbsdown);

        }

        // if question number is under 10
        if (questionNumber < 10) {
            // updates question number
            questionNumber++;
            // called after an answer is given
            updateQuestion();

        } else {

            // Passing values to the results activity
            Intent intent = new Intent(this, RandomTestResults.class);
            intent.putExtra("results", results);
            intent.putExtra("Questions", question);
            intent.putExtra("CorrectAnswer", correctAnswer);
            intent.putExtra("score", score);
            // Start Activity
            this.startActivity(intent);

        }

    }

}

1 个答案:

答案 0 :(得分:0)

使用AlarmManager并在调用时使用finish();