andriod提供了Handler和 Looper 来满足线程间的通信。
Handler先进先出原则。Looper类用来管理特定线程内对象之间的消息交换(MessageExchange)。
public class AppStartUI extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) { // TODO 程序开始启动时的Activity
super.onCreate(savedInstanceState);
setContentView(R.layout.appstart);// 设定启动时的布局文件为appstart
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(AppStartUI.this, WelcomeUI.class);
startActivity(intent);//启动新的Activity
AppStartUI.this.finish();//Call this when your activity is done and should be closed
}
}, 1000);
}
}
boolean android.os.Handler.postDelayed(Runnable r, longdelayMillis)
延时delayMillis毫秒将Runnable插入消息列队,
Runnable将在handle绑定的线程中运行。post是立即插入消息列队,当消息列队处理到该消息时才运行
这是简单的对handler进行使用。
本文介绍了Android中Handler机制的基本原理及应用实例。通过一个简单的启动Activity的例子,展示了如何使用Handler实现延迟执行任务,以及如何利用Handler在主线程和其他线程间进行通信。
1914

被折叠的 条评论
为什么被折叠?



