如何开始新的活动并等待一段时间再开始另一个活动?

时间:2017-06-19 15:53:29

标签: java android

以下代码开始活动" welcome1"并执行Thread()然后启动另一个名为" gps"的活动。 但是当它执行gps活动而不运行welcome1

如何启动welcome1并等待一段时间然后启动活动gps

代码

public class welcome1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads().detectDiskWrites().detectNetwork()
            .penaltyLog().build());
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome1);

    final TextView welcome = (TextView) findViewById(R.id.textView7);
    final TextView person = (TextView) findViewById(R.id.textView9);
    final Intent v = getIntent();
   final String abt = v.getStringExtra("frist_name");
    Runnable myRun=new Runnable() {
        @Override
        public void run() {
            for (int  s=0;s<2;s++){
                final int finalI =s;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                       if (finalI==0)
                       {
                           welcome.setText(welcome2);
                                person.setText(abt);
                       }
                        if (finalI==1)
                                welcome.setText(steps);
                    }
                });
                synchronized (this) {
                    try {
                        wait(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };

    Thread T=new Thread(myRun);
    T.start();
    String number = v.getStringExtra("mobile_number");
    String pwd = v.getStringExtra("pass");

    Intent d = new Intent(getBaseContext(),gps.class);
    d.putExtra("mobile_number",number);
    d.putExtra("pass", pwd);

    startActivity(d);

}

3 个答案:

答案 0 :(得分:1)

您是否尝试使用处理程序?

只需创建一个Runnable即可启动下一个活动,并按以下方式运行:

new Handler().postDelayed(yourRunnable,timeinmillis)

答案 1 :(得分:0)

将此代码复制并粘贴到oncreate()方法中:

//This will wait for 10secs before launching the gps activity
        Handler h = new Handler();
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(welcome1.this, Gps.class));
            }
        }, 10000);

然后运行你的代码,现在应该可以了。

答案 2 :(得分:0)

startActivity(Intent)开始后立即调用

Thread T。因此,它忽略了您创建的Thread T,只是在UI线程中运行。

正确的代码可以执行您想要的操作:

public class welcome1 extends Activity {

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads().detectDiskWrites().detectNetwork()
            .penaltyLog().build());
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome1);

    final TextView welcome = (TextView) findViewById(R.id.textView7);
    final TextView person = (TextView) findViewById(R.id.textView9);
    final Intent v = getIntent();    final String abt = v.getStringExtra("frist_name");
    Runnable myRun=new Runnable() {
        @Override
        public void run() {
            for (int  s=0;s<2;s++){
                final int finalI =s;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                       if (finalI==0)
                       {
                           welcome.setText(welcome2);
                                person.setText(abt);
                       }
                        if (finalI==1)
                                welcome.setText(steps);
                    }
                });
                try {
                    wait(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String number = v.getStringExtra("mobile_number");
                        String pwd = v.getStringExtra("pass");

                        Intent d = new Intent(getBaseContext(),gps.class);
                        d.putExtra("mobile_number",number);
                        d.putExtra("pass", pwd);
                        startActivity(d);
                    }
                });
            }
        }
    };

    Thread T=new Thread(myRun);
    T.start();
}