安卓線程相關 HandlerThread Handler Thread Looper Message Runnable

來源:互聯網
上載者:User

安卓線程相關 HandlerThread Handler Thread Looper Message Runnable
本文由PurpleSword(jzj1993)原創,轉載請註明原文網址 http://blog.csdn.net/jzj1993
安卓主線程(UI線程)是線程不安全的:對UI控制項的操作都應在主線程中完成;UI線程不應執行耗時操作,以免程式不響應(即ANR異常)


實現新線程的常用方法(注意要調用start方法啟動新線程而不是run方法):
一、定義類,實現Runnable介面
class MyRunnable implements Runnable { /** * 實現介面的run方法 */ @Override public void run() { // run方法的中程式體將在新線程中執行 } } new Thread(new MyRunnable()).start();

二、簡潔寫法
new Thread(new Runnable() { @Override public void run() { // 新線程操作 } }).start();

三、定義類,繼承Thread(不推薦):
class MyThread extends Thread { /** * 覆寫run方法 */ @Override public void run() { // 新線程操作 } } new MyThread().start();

Thread和Runnable關係
public class Thread extends Object implements Runnable { }

建立UI線程/主線程的Handler
一、(推薦寫法,用於不處理訊息,只是接收Runnable的情況下)
// 無參數執行個體化時,會建立當前正在運行線程(即主線程)的Handler Handler handler = new Handler();
如果handler需要處理訊息,按照以下寫法將會產生警告This Handler class should be static or leaks might occur。此時可使用方法二)
Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { } } };
二、(缺點在於需要定義一個新的類,寫起來比較麻煩)
// 無參數執行個體化時,會建立當前正在運行線程(即主線程)的Handler private Handler handler = new MyHandler() { // 處理其他線程發送的訊息 @Override public void handleMessage(Message msg) { Log.d("msg", "msg what = " + msg.what); switch (msg.what) { } } };
protected static class MyHandler extends Handler { }
三、(推薦寫法,可用於接收Runnable和處理訊息的情況下)
boolean handleMessage(Message msg) { switch (msg.what) { } return true; }
private final Handler mainHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() { @Override public boolean handleMessage(Message msg) { return ActivityMain.this.handleMessage(msg); } });
在UI線程中執行程式
handler.post(new Runnable(){ @Override public void run() { // 操作UI控制項等... } });

建立新線程的Handler
private HandlerThread handlerThread; private Handler handler;
this.handlerThread = new HandlerThread(getClass().getSimpleName()); this.handlerThread.start(); this.handler = new Handler(this.handlerThread.getLooper(), new Handler.Callback() { public boolean handleMessage(Message msg) { return NewThread.this.handleMessage(msg); } });
/** * 處理訊息 * * @param msg * @return */ private boolean handleMessage(Message msg) { switch (msg.what) { } return true; }
通過Message進行線程間通訊:在任意線程中向其他線程Handler發送訊息
/** * 向線程Handler發送訊息 * * @param id * @param o */ protected void sendMsg(int id, Object o) { if (handler != null) { Message msg = Message.obtain(); msg.obj = o; msg.what = id; handler.sendMessage(msg); } }
/** * 向線程Handler發送訊息 * * @param what */ protected void sendMsg(int what) { if (handler != null) { handler.sendEmptyMessage(what); } }

handler.sendMessageDelayed(msg, milliseconds);
Message.obtain(handler, what, obj).sendToTarget();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.