3.08Handle

3.08 Handle

理解:Handle是SDK处理异步消息的核心类
作用:让子线程通过UI线程通信来更新UI界面

Handle使用

1.new message创建新的Message
2.obtainMessage从消息池获得一个Message对象,sendMessage时发出此Message

代码展示

 new Thread(new Runnable() {
                    @Override
                    public void run() 
                     Thread.sleep(5000);
                        handler.sendMessage(message);
                    }
                }).start();
                //子线程发消息
mHandle=new Handle(){
public void handleMessage(Message msg) 
{ 
if(msg.what==1)
mTextView.setText(“下载完成”);
super.handleMessage(msg);
}
};             
//主线程捕获消息

Looper

Looper:内部包含了消息队列MeaageQueue,所有Handle发送的消息都走向MessageQueue,Looper.loop()死循环,负责不断从MessageQueue消息队列中取消息并发送给目标

MessageQueue

MessageQueue:消息队列,添加、处理消息

Message

定义:消息对象,子线程将需要传递到UI线程的信息放入Message对象中,Message对象可以通过Handle对象的obtainMessage方法获得

属性:

what属性:int类型
arg1,arg2属性:int类型
obj属性:任意类型
sendToTarget属性:指定Handle对象

倒计时任务代码展示

xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.myapplication.Activity_daoji">

    <EditText
        android:id="@+id/daoji_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/daojishi_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_gravity="center"
        android:text="倒计时" />
    <Button
        android:id="@+id/daojishi_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="开始倒计时"/>
LinearLayout>
Java文件
package com.example.administrator.myapplication;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity_daoji extends AppCompatActivity {
    private EditText daojiET;
    private Button daojishiBtn;
    private TextView daojishiTV;
    private int time;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            daojishiTV.setText(msg.what+"");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daoji);

        bindID();

        daojishiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 time = Integer.parseInt(daojiET.getText().toString());//Integer.parseInt将字符串转换成整形
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        for (int i=time;i>0;i--){
                            //跟新UI
//                            Message message=handler.obtainMessage();
//                            message.what=time;
//                            handler.sendEmptyMessage(time);
                            handler.sendEmptyMessage(i);
                            try {
                                Thread.sleep(1000);
                            }catch (InterruptedException e){
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });

    }

    private void bindID() {
        daojiET=findViewById(R.id.daoji_et);
        daojishiBtn=findViewById(R.id.daojishi_btn);
        daojishiTV=findViewById(R.id.daojishi_tv);
    }
}

图片展示
3.08Handle_第1张图片

你可能感兴趣的:(3.08Handle)