Android Handler类 发送消息-post()和postDelay(), Looper讲解

https://blog.csdn.net/weixin_41101173/article/details/79701832

首先,post和postDelay都是Handler的方法,用以在子线程中发送Runnable对象的方法;

其次,Android中post()方法可以直接在非UI线程中更新UI,不同与Handelr的Send类方法,需要进行切换;

最后,两个方法在实现UI线程事件的时间上有所区别,postDelayed()方法用以延期执行,post则是立即执行;

(2)Handler类的post类方法和send类方法联系与区别

①post类方法,以匿名内部类的形式发送Runnable对象,在Runnable对象重写的run()方法中直接对UI进行更新;

new Thread(new Runnable() {
@Override
public void run() {
/**
耗时操作
*/
handler.post(new Runnable() {
@Override
public void run() {
/**
更新UI
*/
}
});
}
}).start();
---------------------
作者:Chin_style
来源:CSDN
原文:https://blog.csdn.net/weixin_41101173/article/details/79701832
版权声明:本文为博主原创文章,转载请附上博文链接!

三种切回主线程的实例:

final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
// 素描算法处理 耗时操作
final Bitmap bitmap1 = SketchUtil.testGaussBlur(finalBitmap,1,1);
final Bitmap bitmap2 = SketchUtil.testGaussBlur(finalBitmap,10,10);
final Bitmap bitmap3 = SketchUtil.testGaussBlur(finalBitmap,20,20);

// 三种切回主线程更新UI的方法
imageView.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap1); // 素描图
}
});

runOnUiThread(new Runnable() {
@Override
public void run() {
orignView.setImageBitmap(bitmap2); // 素描图
}
});

handler.post(new Runnable() {
@Override
public void run() {
threeView.setImageBitmap(bitmap3); // 素描图
}
});
}
}).start();
---------------------
作者:Chin_style
来源:CSDN
原文:https://blog.csdn.net/weixin_41101173/article/details/79701832
版权声明:本文为博主原创文章,转载请附上博文链接!

注意:使用handler方法切回主线程时,注意handler的实例化要放在主线程中,而不能在新开的子线程中,否则报错:

RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

这是因为,Handler在哪里创建,就获得哪里的Looper。主线程创建的Handler,即默认使用主线程的Looper。

常见常用的post()类方法汇总:

post(Runnable)

postAtTime(Runnable,long)

postDelayed(Runnable long)

②send类方法,比如sendMessage()方法,使用该方法发送构造好的Message,然后用Handler的handleMessage()方法接收发送出来的消息,在方法中对UI进行更新;

private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) { //判断标志位
case 1:
/**
获取数据,更新UI
*/
break;
}
}
};


public class WorkThread extends Thread {

@Override
public void run() {
super.run();
/**
耗时操作
*/

//从全局池中返回一个message实例,避免多次创建message(如new Message)
Message msg =Message.obtain();
msg.obj = data;
msg.what=1; //标志消息的标志
handler.sendMessage(msg);
}
}
常见常用的send类方法汇总:
---------------------
作者:Chin_style
来源:CSDN
原文:https://blog.csdn.net/weixin_41101173/article/details/79701832
版权声明:本文为博主原创文章,转载请附上博文链接!

常见常用的send类方法汇总:

sendEmptyMessage(int)

sendMessage(Message)

sendMessageAtTime(Message,long)

sendMessageDelayed(Message,long)

分析:谷歌为Android系统提供了一系列的post类方法用以发送Runnable对象,又提供了一系列的send类方法用以发送Message对象,其实二者并不矛盾也不重复,打开post类方法的源码,就会发现最终发送的Runnable对象也会转变成Message对象进行发送。谷歌提供两类方法应该是分别处理不同的场景,发送的消息较为复杂时,且每种消息对应一种UI的更新时选择使用send类方法;而当子线程中只发出一种消息时,则直接使用post方法发送消息,且直接在post方法的内部实现UI的更新。

(3)Message的构造

public final class Message implements Parcelable {
public int what;
public int arg1;
public int arg2;
public Object obj;
...
}
Message类中有这几个成员变量描述消息,其中what是我们定义的消息码,为了让接收者能知道消息是关于什么的。arg1和arg2用于发送一些integer类型的值。obj用于传输任意类型的值。
---------------------
作者:Chin_style
来源:CSDN
原文:https://blog.csdn.net/weixin_41101173/article/details/79701832
版权声明:本文为博主原创文章,转载请附上博文链接!

⑦Looper的工作原理:Looper在Android的消息机制中扮演着消息循环的角色,具体来说就是它会不停地从MessageQueue中查看是否有新消息,如果有新消息就会立刻处理,否则就一直阻塞在那里。注意关注一些重要的Looper的方法:

Looper.prepare()-为当前线程创建一个Looper;
Looper.loop()-开启消息循环,只有调用该方法,消息循环系统才会开始循环;
Looper.prepareMainLooper()-为主线程也就是ActivityThread创建Looper使用;
Looper.getMainLooper()-通过该方法可以在任意地方获取到主线程的Looper;
Looper.quit() Looper.quitSafely()-退出Looper,自主创建的Looper建议在不使用的时候退出
⑧ActivityThread主线程通过ApplicationThread和AMS进行进程间通信
---------------------
作者:Chin_style
来源:CSDN
原文:https://blog.csdn.net/weixin_41101173/article/details/79701832
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/bluestorm/p/11116669.html