I want a new activity to be started via an intent
after a preset amount of time of inactivity has passed. I used the code pasted below but it still does not work and it stays on the same screen. I know this is not the android-y way to do it, but I'm pretty sure it should work (it makes more sense to me this way). On a side note, could I use this to make something like a textview, button or other object change its visibility from invisible
to visible
? I've looked around and haven't found any answers. Thanks in advance.
public class Welcome extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
Thread thread = new Thread() {
@Override
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//do stuff
Intent myIntent = new Intent(Welcome.this,MainActivity.class);
Welcome.this.startActivity(myIntent);
overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
}
});
}
};
You could do that with a handler :
new Handler().postDelayed(<your runnable>, delayInMillis);
For updating widgets, it's not a very good idea to do it that way. You would be better using an AsyncTask if the delay is not very large.
You can make a thread and make it sleep for that interval of time. After that time your new intent will be started.
Eg:-
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
Intent intent = new Intent(First_Intent.this,Second_Intent.class );
startActivity(intent);
}
}
};
splashTread.start();
}
You can try this...
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Do your thing......
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
}
}, 3000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.