Reputation: 13747
I want to create an activity that opens when I start my app, wait some time and jumps to the next activity without the user pressing anything.
Here is my code:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread thread = new Thread();
thread.start();
}
public class waitSeconds extends Thread {
public void run() {
Log.i("MyActivity", "MyClass");
try {
wait(300);
Intent intent = new Intent(MainActivity.this, main_window.class);
startActivity(intent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
As it seems that it is never going to the "run" method.
How can I do this?
Upvotes: 2
Views: 2054
Reputation: 71
public class SplashScreenActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Thread() {
public void run() {
try {
Intent i = new Intent(SplashScreenActivity.this,
MainActivity.class);
Thread.sleep(2000);
startActivity(i);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}
Upvotes: 1
Reputation: 7888
include this in your Activity:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SplashHandler handler=new SplashHandler();
Message msg = new Message();
msg.what = 0;
handler.sendMessageDelayed(msg, 3000);
}
private class SplashHandler extends Handler {
public void handleMessage(Message msg)
{
switch (msg.what)
{
default:
case 0:
super.handleMessage(msg);
Intent intent = new Intent(MainActivity.this,main_window.class);
startActivity(intent);
MainActivity.this.finish();
}
}
}
Upvotes: 1