前言
Handler是Android应用比较广泛的对象,了解Handler的作用和如何运用,对理解Android有很大的作用。
Handler是什么
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler it is bound to a Looper. It will deliver messages and runnables to that Looper's message queue and execute them on that Looper's thread. Handler允许您发送和处理与线程MessageQueue关联的消息和可运行对象。每个Handler实例都与一个线程和该线程的消息队列相关联。创建新的Handler时,它将绑定到一个Looper。它将消息和runnables传递到该Looper的消息队列,并在该Looper的线程上执行它们。
Handler的作用
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed at some point in the future; and (2) to enqueue an action to be performed on a different thread than your own. Handler有两个主要用途:(1)安排消息和runnables在将来某个时候执行;(2)安排在不同线程上执行动作。
Handler的简单运用
下面的代码片段展示Handler最常用的场景,主要作用是派发消息。下面的handler使用MainActivity的Looper,其Thread ID也和MainActivity相同。
public class MainActivity extends AppCompatActivity {
private final static String TAG = "HandlerTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate: PID = " + Os.getpid() + ", TID = " + Os.gettid());
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
Log.i(TAG, "handleMessage: msg.what = " + msg.what);
Log.i(TAG, "handleMessage: PID = " + Os.getpid() + ", TID = " + Os.gettid());
switch (msg.what) {
case 1:
Log.i(TAG, "handleMessage: do something here");
break;
default:
break;
}
}
};
@Override
protected void onResume() {
super.onResume();
mHandler.sendEmptyMessage(1);
}
}
创建新的Handler Thread
下面的代码创建了新的Handler Thread,注意:下面的Handler处在新的thread中,其Thread ID与MainActivity的Thread ID不同,如果需要在此Handler中操作当前Activity的UI,需要呼叫runOnUiThread(), 在runOnUiThread()中,其Thread ID与MainActivity一样。
public class MainActivity extends AppCompatActivity {
private final static String TAG = "HandlerTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate: PID = " + Os.getpid() + ", TID = " + Os.gettid());
}
Handler backgroundHandler;
@Override
protected void onResume() {
super.onResume();
HandlerThread backgroundHandlerThread = new HandlerThread("BackgroudHandlerThread");
backgroundHandlerThread.start();
backgroundHandler = new Handler(backgroundHandlerThread.getLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
Log.i(TAG, "background handleMessage: msg.what = " + msg.what);
Log.i(TAG, "background handleMessage: PID = " + Os.getpid() + ", TID = " + Os.gettid());
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "runOnUiThread: PID = " + Os.getpid() + ", TID = " + Os.gettid());
}
});
}
};
backgroundHandler.sendEmptyMessage(2);
}
}
代码
感兴趣的读者可以从gitee下载测试代码, https://gitee.com/guangwei_jiang/handler-test
小结
Handler可以十分方便应用于消息派发中,默认情况下,其运行在Activity的main thread中;如果需要运行在新的thread中,需要使用HandlerThread()创建新的thread, 并将其Looper赋给Handler。
|