一、Handler的定义
是异步通信的类,主要接受子线程发送的数据, 并用此数据配合主线程更新UI.
二、Handler主要通过Looper和MessageQueue来完成更新UI,那么什么是Looper和MessageQueue呢
1、Looper:
每个线程只能够有一个Looper,Looper负责创建并管理当前线程中的MessageQueue,调用loop方法后就会在一个无限循环体中不断地从MessageQueue中取出Message并分发给对应的Handler,最后回调handleMessage()方法处理此消息。Looper才是整个机制的核心!
2、MessageQueue:
消息队列,先进先出管理Message,在初始化Looper对象时会创建一个与之关联的MessageQueue
3、Message:
其创造的message是Handler接收与处理的对象。
3、Handler更新UI的步骤:
在主线程中创建Handler并重写handleMessage()方法在任何线程中都可以利用此Handler发送消息,消息会被发送到主线程MessageQueue中一旦MessageQueue中有新消息,主线程中的
Looper 就会发现此消息,然后就会调用Handler的handleMessage()方法处理此消息
如图
三、如何使用Handler呢
1、创建handler对象:
private Handler handler = new Handler(){
@Override
//书写handleMessage方法,用于第三步接收并处理信息
public void handleMessage(Message msg) {
super.handleMessage(msg);
//这里是发送消息后对消息进行处理
int arg1 = msg.arg1;
int arg2 = msg.arg2;
String tip = (String) msg.obj;
if (msg.what==1){
//更新UI
downLoadTip.setText("下载完成"+arg1+arg2+tip);
}
}
};
2、发送消息
//创建子线程
new Thread(new Runnable() {
@Override
public void run() {
try {
//子线程睡眠5秒
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//发送更新UI的信息
Message message = handler.obtainMessage();
//发送信息的三种方法
message.what = 1; //只能为int型
message.arg1 =2; //只能为int型
message.arg2 = 3;
message.obj = "keep smile"; //可以是字符串,对象等
handler.sendMessage(message);
}
}).start(); //启动线程
3、接收并处理消息
private Handler handler = new Handler(){
@Override
//书写handleMessage方法,用于第三步接受信息
public void handleMessage(Message msg) {
super.handleMessage(msg);
//这里是发送消息后对消息进行处理
int arg1 = msg.arg1;
int arg2 = msg.arg2;
String tip = (String) msg.obj;
if (msg.what==1){
//更新UI
downLoadTip.setText("下载完成"+arg1+arg2+tip);
}
}
};
四、使用handler类完成倒计时案例
1、构建倒计时布局activity_ji_shi.xml,注意设置ID
<?xml version="1.0" encoding="utf-8"?>
<!--构建倒计时布局,注意设置ID-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.JiShiActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center"
android:textSize="20sp"
android:text="设置时间"/>
<EditText
android:id="@+id/edit_et"
android:layout_width="52dp"
android:layout_height="match_parent" />
<TextView
android:layout_width="30dp"
android:layout_height="match_parent"
android:textSize="20sp"
android:gravity="center"
android:text="秒"/>
</LinearLayout>
<TextView
android:id="@+id/daojishi_tv"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="倒计时:"
android:textSize="20sp"
android:layout_centerInParent="true"
android:gravity="center"/>
<TextView
android:id="@+id/current_tv"
android:layout_width="100dp"
android:layout_height="50dp"
android:text=""
android:textSize="20sp"
android:gravity="center"
android:layout_alignTop="@+id/daojishi_tv"
android:layout_toRightOf="@+id/daojishi_tv" >
</TextView>
<Button
android:id="@+id/start_btn"
android:layout_width="wrap_content"
android:text="开始计时"
android:layout_below="@+id/daojishi_tv"
android:layout_alignLeft="@+id/daojishi_tv"
android:gravity="center"
android:layout_height="50dp" />
</RelativeLayout>
2、/在JiShiActivity中完成对EditText、TextView、Button的绑定,并完成对button按钮的监听,按钮监听事件中完成子线程的创建,和通知主线程更新UI
public class JiShiActivity extends AppCompatActivity {
//创建handler对象
private Handler handler = new Handler() {
@Override
//书写handleMessage方法,接受信息
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what!=0){
currentTV.setText(msg.what + "");
}else{
//通知主线程更新UI
currentTV.setText("计时结束");
}
}
};
//定义控件
private EditText editText;
private TextView currentTV;
private Button startBtn;
//定义全局变量i,用于for语句
private int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ji_shi);
bangID();
//对button按钮进行监听
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取editText中输入的值,装换成字符串
String str = editText.getText().toString();
//将字符串转换成int型
i = Integer.parseInt(str);
new Thread(new Runnable() {
@Override
public void run() {
//使用for语句实现倒计时
for (int time = i; i>0||i ==0; i--) {
Message message = handler.obtainMessage();
message.what = i;
handler.sendMessage(message);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
}
private void bangID() {
editText = findViewById(R.id.edit_et);
currentTV = findViewById(R.id.current_tv);
startBtn = findViewById(R.id.start_btn);
}
}