[英]Make an intent start after a preset amount of time
我希望在预设的不活动时间过后,通过intent
启动新活动。 我使用下面粘贴的代码,但它仍然不起作用,它保持在同一个屏幕上。 我知道这不是android-y的方法,但我很确定它应该可行(这对我来说更有意义)。 在旁注中,我是否可以使用它来制作类似文本视图,按钮或其他对象的内容,将其可见性从不invisible
变为visible
? 我环顾四周,没有找到任何答案。 提前致谢。
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);
}
});
}
};
您可以使用处理程序执行此操作:
new Handler().postDelayed(<your runnable>, delayInMillis);
对于更新小部件,以这种方式执行它并不是一个好主意。 如果延迟不是很大,你最好使用AsyncTask 。
您可以创建一个线程并使其在该时间间隔内休眠。 在那之后,您的新意图将开始。
例如:-
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();
}
你可以尝试这个......
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.