0

can you help me with example code of a time limit. can anyone here show complete code exactly how to implmeent or have a time limit in one class that will intent in the next class if the user didnt click the button. scenario- within 5 sec the user must click the button to intent in another class but if he reached the 5 sec time limit he will be intent to mainmenu.class - its like a game that you must be fast clicking buttons :) hope can help me

a.setOnClickListener(new View.OnClickListener() {

        @Override   
           public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"CORRECT!",
                        Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(),EasyTwo.class);
                startActivity(intent);
user2630787
  • 65
  • 1
  • 8
  • use a countdown timer http://stackoverflow.com/questions/17839419/android-thread-for-a-timer/17839725#17839725 and this http://developer.android.com/reference/android/os/CountDownTimer.html – Raghunandan Jul 31 '13 at 09:14
  • what i mean is a time limit that will automatically intent to the next class. – user2630787 Jul 31 '13 at 09:19
  • what is mean is use a countdown timer when its done navigate to next activity. – Raghunandan Jul 31 '13 at 09:20

2 Answers2

0

An intent is an object whose reference can be modified as any other java object

  1. Create an instance variable of Intent Object

    Intent intent ;

  2. Assign it the class value that you want to assign

    intent = new Intent(getApplicationContext(),EasyTwo.class);

  3. Use a count down timer to check if 5 seconds have passed or not and if passed assign the instance of intent a new value

Example:

public class MainActivity extends Activity {
Intent intent ;
CountDownTimer cdt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn = (Button) findViewById(R.id.button1);
    // The 5000 is 5 seconds
    // The 1000 is 1 second interval which is not important in yout case
    cdt = new CountDownTimer(5000,1000) {

        @Override
        public void onTick(long millisUntilFinished) {}

        @Override
        public void onFinish() {
            intent = new Intent(getApplicationContext(),mainmenu.class);
        }
    };

    intent = new Intent(getApplicationContext(),EasyTwo.class);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(intent);
            //Stop timer as button clicked
            cdt.cancel();
        }
    });

    //Start the timer
    cdt.start();
}
}
Girish Nair
  • 5,148
  • 5
  • 40
  • 61
  • thank you very much! but why is it it keeps going in the desktop screen of the android. is that because of the finish()? – user2630787 Jul 31 '13 at 09:42
  • @user2630787 There is no finish in my code ??? The only one is `onFinish()` method which is called by the CountDownTimer once the count of 5 seconds is done – Girish Nair Jul 31 '13 at 09:45
  • thank you! :) i have a question.@Override public void onFinish() { intent = new Intent(getApplicationContext(),mainmenu.class); -after the count of 5 seconds it will be intent in the mainmenu class right? – user2630787 Jul 31 '13 at 09:52
  • there is no error but it keeps giving me force close :(((( i know that your code is correct. but whyyyyyyyyyyyyyyyyyy :(( – user2630787 Jul 31 '13 at 09:56
  • waaaaaaaaaaaaaaaaaa THANK YOU VERYYYY MUCH!!! YOURE A LIFE SAVER!!!!!!!!!!!!!!!!!!!!!!!!!!! IT WORKS. stupid me i forgot to change the layout :) – user2630787 Jul 31 '13 at 10:07
0

You can try to use a handler and give your duration in it. It might solve your problem..

WISHY
  • 11,067
  • 25
  • 105
  • 197