异步消息处理(Message, Handler, MessageQueue, Looper)
AsyncTask 适用于单线程任务处理,多任务处理还是 Message/Handler 处理方便一些
主要使用方式:
1,创建子类继承自 Handler 类,覆盖 handleMessage(Message) 方法,switch Message 对象的 what(int 类型) 域等携带的信息,用于在 UI 线程中进行对应的操作;
2,new 一个 Handler 对象,并保存,之后发送 Message 对象会用到;
3,子线程中进行耗时操作,完成后 new 一个 Message 对象,设置 what, obj(可选)域;
4,使用 Handler 对象的 sendMessage(Message) 方法即可将 Message 发送给 Handler 对象。
这看起来像是回调,不过差异还是挺大的。主要的区别就是 Handler 对象的 sendMessage(Message) 方法只是通知 Handler 对象,之后 handlerMessage(Message) 会自动在 UI 线程中被调用,而直接调用对应方法则不会回到 UI 线程。
package club.seliote.demo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int MESSAGE_UPDATE_0 = 0;
private static final int MESSAGE_UPDATE_1 = 1;
private static final int MESSAGE_UPDATE_2 = 2;
private static final int MESSAGE_UPDATE_3 = 3;
private TextView mTextView;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
this.mTextView = (TextView) this.findViewById(R.id.text_view);
/**
* Step 2
*/
this.mHandler = new DownloadHandler();
this.download();
}
private void download() {
for (int i = 0; i < 100; i++) {
/**
* Step 3
*/
new Thread(() -> {
try {
// 模拟下载
Thread.sleep((long) (Math.random() * 10000));
}
catch (InterruptedException interruptedException) {
Log.e(TAG, Thread.currentThread().getId() + " had interrupted!");
Message message = new Message();
message.what = 4;
message.obj = this.mTextView;
this.mHandler.sendMessage(message);
return;
}
Message message = new Message();
message.what = new Random().nextInt(4);
message.obj = this.mTextView;
/**
* Step 4
*/
this.mHandler.sendMessage(message);
}).start();
}
}
/**
* Step 1
*/
private class DownloadHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 判断具体的消息类型,进行相应处理
switch (msg.what) {
case MainActivity.MESSAGE_UPDATE_0:
// Object 类型参数可以用来传递要更新的对象
((TextView)(msg.obj)).setText("000000");
((TextView)(msg.obj)).setTextColor(MainActivity.this.getResources().getColor(R.color.red));
break;
case MainActivity.MESSAGE_UPDATE_1:
((TextView)(msg.obj)).setText("111111");
((TextView)(msg.obj)).setTextColor(MainActivity.this.getResources().getColor(R.color.black));
break;
case MainActivity.MESSAGE_UPDATE_2:
((TextView)(msg.obj)).setText("222222");
((TextView)(msg.obj)).setTextColor(MainActivity.this.getResources().getColor(R.color.blue));
break;
case MainActivity.MESSAGE_UPDATE_3:
((TextView)(msg.obj)).setText("333333");
((TextView)(msg.obj)).setTextColor(MainActivity.this.getResources().getColor(R.color.grey));
break;
default:
((TextView)(msg.obj)).setText("-1-1-1-1-1-1");
((TextView)(msg.obj)).setTextColor(MainActivity.this.getResources().getColor(R.color.colorAccent));
break;
}
}
}
}
异步消息处理(Message, Handler, MessageQueue, Looper)的更多相关文章
- (转)Android消息处理机制(Handler、Looper、MessageQueue与Message)
转自 http://www.cnblogs.com/angeldevil/p/3340644.html Android消息处理机制(Handler.Looper.MessageQueue与Messag ...
- Android开发之异步消息处理机制Handler
更加详细的介绍Handler的博文-http://blog.csdn.net/guolin_blog/article/details/9991569 Android中的异步消息处理主要有四个部分组成, ...
- [转]Handler MessageQueue Looper消息循环原理分析
Handler MessageQueue Looper消息循环原理分析 Handler概述 Handler在Android开发中非常重要,最常见的使用场景就是在子线程需要更新UI,用Handler ...
- Android消息处理机制(Handler、Looper、MessageQueue与Message)
Android是消息驱动的,实现消息驱动有几个要素: 消息的表示:Message 消息队列:MessageQueue 消息循环,用于循环取出消息进行处理:Looper 消息处理,消息循环从消息队列中取 ...
- Android:异步处理之Handler、Looper、MessageQueue之间的恩怨(三)
前言 如果你在阅读本文之前,你不知道Handler在Android中为何物,我建议你先看看本系列的第一篇博文<Android:异步处理之Handler+Thread的应用(一)>:我们都知 ...
- 异步消息处理机制Handler
Android 中的异步消息处理主要由四个部分组成,Message.Handler.MessageQueue 和Looper. 1. Message Message 是在线程之间传递的消息,它可以在内 ...
- Android Handler MessageQueue Looper 消息机制原理
提到Android里的消息机制,便会提到Message.Handler.Looper.MessageQueue这四个类,我先简单介绍以下这4个类 之间的爱恨情仇. Message 消息的封装类,里边存 ...
- 异步消息处理机制——Handler用法
Handler 1. Message Messsge是线程之间传递的消息,它可以在内部携带少量的信息,用于在不同线程之间交换数据,Message的what字段,除此之外还可以使用arg1和arg2字段 ...
- Handler 、 Looper 、Message异步消息处理线程机制( hander消息机制原理)
Handler . Looper .Message 这三者都与Android异步消息处理线程相关的概念. 那么什么叫异步消息处理线程呢? 异步消息处理线程启动后会进入一个无限的循环体之中,每循环一次, ...
随机推荐
- 时间序列算法理论及python实现(2-python实现)
如果你在寻找时间序列是什么?如何实现时间序列?那么请看这篇博客,将以通俗易懂的语言,全面的阐述时间序列及其python实现. 时间序列算法理论详见我的另一篇博客:时间序列算法理论及python实现 - ...
- C++中的RAII(转)
转自https://blog.csdn.net/wangshubo1989/article/details/52133213 有很多东西我们一直在用,但是不知道他的名字. 什么是RAII? RAII是 ...
- 【luogu P3369 【模板】普通平衡树(Treap/SBT)】 题解 pb_ds
我永远都爱STL ! 我爱PB_DS ! #include <iostream> #include <cstdio> #include <ext/pb_ds/tree_p ...
- 【luogu P2299 Mzc和体委的争夺战】 题解
题目链接:https://www.luogu.org/problemnew/show/P2299#sub 裸的迪杰斯特拉(我是在考试前复习一下板子) #include<iostream> ...
- 我和我的广告前端代码(六):webpack工程合并、也许我不需要gulp
随着年初开始使用webpack重构公司的广告代码,已经有将近一年的时间了,需求也渐渐的稳定了.我想也是时候将这几个工程整理一下,顺带着处理一些历史问题. 由于当年各个业务线没有整合.需求也没有固定,考 ...
- sql server 自增长显式添加值
如果想在自增列添加数据,会提示我们不能插入显式值 解决:
- 使用补丁破解IntelliJ IDEA 2017收费版本(转)
1. 首先去官网http://www.jetbrains.com/idea/download/#section=windows下载Ultimate版(注意不是community版)下载并安装.一定要记 ...
- Sass 基础(六)
join() 函数 join()函数是将两个列表连接合并成一个列表. >>join(10px 20px, 30px 40px) (10px 20px 20px 40px) >> ...
- 判断js中数据类型 的最短代码
判断字符串类型的: function isString(obj) { return obj+"" === obj; } 判断bool类型的: function isBool(obj ...
- [POI2008]MAF-Mafia(图论,贪心)
题目描述 Mob feud rages in Equatorial Byteotia. The mob bosses have come to the country's capital, Byteb ...