Handler的执行流程
说白了就是子线程调用了主线程的某对象的方法,通过这个运行在主线程的对象,就能够对UI界面进行更新。
自己也可以实现,不过Handler更为专业的对象,已经写好了。
相关名词解析
UI线程、Handler、Message、Message Queue、Looper。
Looper.getMainLooper()得到的是主线程的Looper对象,Looper.myLooper()得到的是当前线程的Looper对象。
Handler相关常用方法
Handler写在子线程中
class LooperThread extends Thread {
public Handler mHandler;
public void run() {
Looper.prepare(); // 创建本线程的Looper并创建一个MessageQueue
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop(); // 开始运行Looper,监听Message Queue
}
}
子线程更新UI
Android在子线程更新UI的7种方法
Message8种构造方法
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- yrrf.cn 版权所有 赣ICP备2024042794号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务