android - How to time an activity out after a certain timeframe? -
i want create application in user has 90 seconds in order complete number of sums.
i unsure how stop activity , move after timeframe up?
activity code:
/** * class holding activity has 10 random sums user 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 random setuprandom(); // set text view equal question in array text.settext(question[questionnumber - 1]); // set on click listener submit button submit.setonclicklistener(this); // updatequestion updatequestion(); } /** * method 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 creates random sum user answer */ public void setuprandom() { // setting new random random random = new random(); // generating random number between 1 , 12 random1 = random.nextint(12) + 1; // generating random number between 1 , 12 random2 = random.nextint(12) + 1; // creating random question string question[questionnumber - 1] = random1 + " x " + random2 + " = "; // creating correct answer question correctanswer[questionnumber - 1] = random1 * random2; } /** * method 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 entered in edittext final string entry = answer.gettext().tostring(); // convert string value int int = integer.parseint(entry); // // setting user answer equal correct part of results array results[questionnumber - 1] = a; // if user answer equal correct answer 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 under 10 if (questionnumber < 10) { // updates question number questionnumber++; // called after answer given updatequestion(); } else { // passing values 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); } }
}
use alarmmanager , when calls use finish();
Comments
Post a Comment