如何让活动等待,然后在Android中自行跳转到下一个活动?

问题描述 投票:2回答: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();
        }
    }
}

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

我该怎么做?

java android multithreading wait
2个回答
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();
                }
              }
        }

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

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.