Android handler的问题:
package mars.handler;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;public class HandlerTest extends Activity {
private Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// sendMessage();
handler.post(r);
setContentView(R.layout.main); //System.out.println("1: " + System.currentTimeMillis());
// Thread t = new Thread(r) ;
// t.start();
System.out.println("a--->" + System.currentTimeMillis());
System.out.println("activity--->" + Thread.currentThread().getId());
System.out.println("activityname--->" + Thread.currentThread().getName());
}
Runnable r = new Runnable() {
public void run() {
//System.out.println("2: " + System.currentTimeMillis());
System.out.println("handler--->" + Thread.currentThread().getId());
System.out.println("handlername--->" + Thread.currentThread().getName()); try {
Thread.sleep(10000);
System.out.println("r2---?" + System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}按照执行的顺序:怎么这条语句:setContentView(R.layout.main);在两条System.out.println之前。而setContentView(R.layout.main);却是最后执行的?这是为什么?
package mars.handler;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;public class HandlerTest extends Activity {
private Handler handler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// sendMessage();
handler.post(r);
setContentView(R.layout.main); //System.out.println("1: " + System.currentTimeMillis());
// Thread t = new Thread(r) ;
// t.start();
System.out.println("a--->" + System.currentTimeMillis());
System.out.println("activity--->" + Thread.currentThread().getId());
System.out.println("activityname--->" + Thread.currentThread().getName());
}
Runnable r = new Runnable() {
public void run() {
//System.out.println("2: " + System.currentTimeMillis());
System.out.println("handler--->" + Thread.currentThread().getId());
System.out.println("handlername--->" + Thread.currentThread().getName()); try {
Thread.sleep(10000);
System.out.println("r2---?" + System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}按照执行的顺序:怎么这条语句:setContentView(R.layout.main);在两条System.out.println之前。而setContentView(R.layout.main);却是最后执行的?这是为什么?
解决方案 »
- 安卓与电视的交互(超难,心理承受能力差者勿进)
- 我是做WEB开发的,平时总看到很多网站都有android版的,这是这么回事? 是android客户端吗?
- Launcher修改问题请教
- cygwin make -v命令报错
- 求高人指点notepad里的一些东西
- 三方协议该不该签?
- android 模拟器能否实现屏幕翻转?
- 可不可以用反射机制来获取android底层传递的数据,求大神!!!
- 如何实现当切换到wifi网络时候自动同步上传照片呢?
- 急--(新人)创建项目时项目名出现感叹号,V7出现X
- 开发安卓程序怎么用 phonegap 做到 连接网站内容
- 急:android动画 如何绕Y轴旋转,求代码
这是异步过程,也就是说启动的r方法是异步线程启动的方法,可能会在 setContentView前,也可能在setContentView后,如果要实现LZ想要的那种效果,是同步,也就是按代码逐个执行