2

我想创建一个在我启动应用程序时打开的活动,等待一段时间并跳转到下一个活动,而无需用户按任何操作。

这是我的代码:

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();
        }
    }
}

似乎它永远不会采用“运行”方法。

我怎样才能做到这一点?

4

2 回答 2

1

你需要这样的东西:

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();

    }

}
于 2012-07-16T07:59:34.050 回答
1

将此包含在您的活动中:

 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();
                }
              }
        }
于 2012-07-16T07:51:13.860 回答