初识Handler
一.什么是线程
线程(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。
选自《维基百科》
二.应用场景
1.铺垫
在Android开发中,我们需要从数据库或者网络中查询消息,可能会耗时很长时间(ANR超时5秒),让我们现在主(ui)线程中测试下,每一次请求都需要6秒才能处理完成,这时候界面会卡主,一些大兄弟脾气不好,连点几下就这样了
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("123","main Thread-->"+Thread.currentThread().getName());
try {
Thread.sleep(6000);
textView.setText("do it");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
这个时候我们就想需要单开一个线程处理,用java的处理方式可以吗?
我们来测试下
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("123","main Thread-->"+Thread.currentThread().getName());
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(6000);
textView.setText("do it");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
});
这又抛异常了,只有在最初绘制视图的线程才能更新UI。
当第一次启动一个Android程序时,Android会自动创建一个称为"main"主线程的线程。
这个主线程(也称为UI线程)很重要,因为它负责把事件分派到相应的控件,其中就包括屏幕绘图事件,它同样是用户与Andriod控件交互的线程。这次解决问题就需要用到Handler了,这也是最常用Handler的地方子线程与UI线程通信。
A Handler allows you to send and process {@link Message} and Runnable
There are two main uses for a Handler: (1) to schedule messages and
runnables to be executed as some point in the future;
(2) to enqueue an action to be performed on a different thread than your own.
Handler有post(Runnable) 和 sendMessage(Message)两种常用方法。
package com.example.handlerapplication;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button mButton;
private TextView textView;
//这个Handler在UI Thread初始化
private Handler uiHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
mButton = (Button) findViewById(R.id.test_btn);
textView = (TextView) findViewById(R.id.textview);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("123", "main Thread-->" + Thread.currentThread().getName());
new Thread(new DownLoad()).start();
}
});
}
class DownLoad implements Runnable {
@Override
public void run() {
Log.d("123", " run Thread-->" + Thread.currentThread().getName());
uiHandler.post(new Runnable() {
@Override
public void run() {
Log.d("123", "Thread-->" + Thread.currentThread().getName());
try {
//模拟网络延迟请求
Thread.sleep(5000);
textView.setText("success");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}
public class MainActivity extends AppCompatActivity {
private Button mButton;
private TextView textView;
private Handler uiHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
Log.d("123", "main Thread-->" + Thread.currentThread().getName());
textView.setText(msg.obj.toString());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
mButton = (Button) findViewById(R.id.test_btn);
textView = (TextView) findViewById(R.id.textview);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new DownLoad()).start();
}
});
}
class DownLoad implements Runnable {
@Override
public void run() {
Log.d("123", "main Thread-->" + Thread.currentThread().getName());
try {
//模拟网络延迟请求
Thread.sleep(5000);
Message message=new Message();
message.obj="success";
uiHandler.sendMessage(message);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}