Reputation: 55
What code should I put if I want to have a class that has a timer that will intent to other class? but it has a button that will automatically intent to other class if I click the button. its like a game, that if you don't click the button you will be transferred to the other class because it has a time limit example is:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easyone);
a = (ImageButton) findViewById(R.id.ib_a);
b = (ImageButton) findViewById(R.id.ib_b);
c = (ImageButton) findViewById(R.id.ib_c);
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);
}
});
}
Thread timer = new Thread(){
protected void timeout() {
try {
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
Intent intent = new Intent(getApplicationContext(),TimesUp.class);
startActivity(intent);
}
}
};start();
}
}//this one
I don't know what code should I input so I put the sleep though I know it is wrong
I have an error on the last bracket I'm very noob in brackets can you help me?
Upvotes: 0
Views: 87
Reputation: 113
Use this for reference
private void timeOutCheck() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//call intent here
}
}, 5 * 1000);
}
Upvotes: 0
Reputation: 2482
You can use an timeout just like this:
private void timeout() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getApplicationContext(),MainMenu.class);
startActivity(intent);
}
}
}, 5000);
I use this method all the time and I never had any problems.
Upvotes: 1
Reputation: 44571
From what I understand you are trying to do is you want to go to the next Activity
if the Button
is clicked or if the time is up.
that if you dont click the button you will be transfered to the other class because it has a time limit
With your code, the Thread
won't run if the Button
isn't clicked so you need to move that code outside of the Listener
for it to run when the Button
isn't clicked, wherever you want it to start.
so i put the sleep though i know it is wrong
sleep()
is ok because it is on a background Thread
Upvotes: 0