public class Welcome extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.welcome); MyRunnable r = new MyRunnable(); handler.postDelayed(r, 2000); handler.postAtTime(r, System.currentTimeMillis() + 100000);
} Handler handler = new Handler();
class MyRunnable implements Runnable { @Override public void run() { Intent intent = new Intent(); intent.setClass(Welcome.this, SecondActy.class); startActivity(intent); }
}}postDelayed和postAtTime,这个是用着欢迎页面的自动跳转
楼上前辈,隔几秒postAtTime也能实现,具体有什么区别?
请查看doc resources list of articles.update the ui from a timer
上面的关于 postAtTime 的解释都是错的~ 请参考api: ---------------------- public final boolean postAtTime (Runnable r, long uptimeMillis)Since: API Level 1 Causes the Runnable r to be added to the message queue, to be run at a specific time given by uptimeMillis. The time-base is uptimeMillis(). The runnable will be run on the thread to which this handler is attached.---------------------- 注意时间基数是uptimeMillis().
还是有些不明白,比如我设置一个Button的点击事件 new Thread(new Runnable() {
和
延迟时间我也知道能不能用代码来演示一下这2种的区别?
public class Welcome extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
MyRunnable r = new MyRunnable();
handler.postDelayed(r, 2000);
handler.postAtTime(r, System.currentTimeMillis() + 100000);
}
Handler handler = new Handler();
class MyRunnable implements Runnable { @Override
public void run() {
Intent intent = new Intent();
intent.setClass(Welcome.this, SecondActy.class);
startActivity(intent);
}
}}postDelayed和postAtTime,这个是用着欢迎页面的自动跳转
请参考api:
----------------------
public final boolean postAtTime (Runnable r, long uptimeMillis)Since: API Level 1
Causes the Runnable r to be added to the message queue, to be run at a specific time given by uptimeMillis. The time-base is uptimeMillis(). The runnable will be run on the thread to which this handler is attached.----------------------
注意时间基数是uptimeMillis().
new Thread(new Runnable() {
@ Override
public void run() {
// TODO Auto-generated method stub
Message msg = Message.obtain();
msg.what = 3;
handler.sendMessageAtTime(msg, 10000);
}
}).start();
然后再设置
private Handler handler = new Handler() {
@ Override
public void handleMessage(android.os.Message msg) {
System.out.println("--what-->"+msg.what);
};
};
我一点击button的时候,logcat中马上显示"--what-->3",并没有延迟啊~?