我想创建一个在我启动我的应用程序时打开的活动,等待一段时间,然后跳转到下一个活动,而无需用户按下任何按钮。
这是我的代码:
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();
}
}
}
似乎似乎永远不会使用“运行”方法。
我该怎么做?
将此包含在您的活动中:
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();
}
}
}
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();
}
}