Reputation: 13
This below code to start activity "welcome1" and execute Thread() Then start another activity called "gps" but when it executes the gps activity running without running activity welcome1
how can to start welcome1 and wait some time then start activity gps
Code
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);
}
Upvotes: 0
Views: 1901
Reputation: 179
startActivity(Intent)
is called right after the Thread T
starts. Thus it ignores the Thread T
you created and just runs in the UI thread.
Correct code to do what you want is this:
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();
}
Upvotes: 0
Reputation: 1985
Copy and paste this code into your oncreate() method:
//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);
Then run your code, it should work now.
Upvotes: 0
Reputation: 782
Have you tried with a Handler?
Just create a Runnable which starts the next activity and run like this:
new Handler().postDelayed(yourRunnable,timeinmillis)
Upvotes: 1