package cc.line;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;public class line extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler.post(line);
setContentView(R.layout.main);
System.out.println("main"+Thread.currentThread().getId());
}
Handler handler=new Handler();
Runnable line=new Runnable() {
public void run() {
System.out.println("run"+Thread.currentThread().getId()); try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}为什么输出的先是main,后是run
setContentView(R.layout.main);这句话是在handler.post()后面运行的,在Activity显示之前main就输出了 这是为什么?
解决方案 »
- android自带的计算器(Calculator),核心计算部分在哪儿?怎么改?
- 项目中需要开发android播放器,需要能缓冲网络视频资源及在线播放,请教各位大牛!
- 关于WebView资源加载的问题
- 关于android的MapView问题,求教大神解救
- Android中animation坐标的问题
- android手机linux内核里的fb0、fb1到底是干嘛的?
- Android 源码编译错误
- IPC(进程间通信),Remote Call(远程调用),IDL(接口描述语言)架构及其在BMP(BREW移动平台),Android中的应用浅析
- andriod 应用开发关于商品列表
- 求大牛指导安装android SDK
- 自己为什么没想到呢。 android
- 在android程序中,如何对一个视频进行加密解密
不知道别的朋友是怎么理解的。
HandlerThread ht = new HandlerThread("King");
ht.start();
handler = new Handler(ht.getLooper())
2.handler.post(line) 是把line 这个runnalbe 发送到 UI线程的消息队列中。而消息队列是先进先出的。
3.在line到达UI的消息队列之前,已经有一个消息在执行了。这就是 UI主线程在执行,其执行顺序是:
onCreate()-> onStart()-> onResume() ->run
只有到达run区域时, 才会从消息队列取出新的信息,进行操作。所以无论你把 handler.post(line)放在哪里,都先看到main 然后才是line楼主可将
System.out.println("run"+Thread.currentThread().getId()); try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
放到onCreate方法里,试试就知道了。
你是先停了10秒再setContentView的吧
2.1的avd运行2.1的工程没问题,2.2的avd运行2.2的工程一直停留再开机界面,控制台一直是
Waiting for HOME ('android.process.acore') to be launched...
哪位大侠帮我解决下啊
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}哥 sleep 是Thread类得一个静态方法啊
你在在onCreate中,就是让UI线程睡10秒啊
[/Quote]