Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[Android] 多執行緒 - Handler和Thread的關係

[Android] 多執行緒 - Handler和Thread的關係

講解初學者搞不清楚的ANR等錯誤

Johnny Sung

August 22, 2013
Tweet

More Decks by Johnny Sung

Other Decks in Programming

Transcript

  1. ໹ᜐᚕ • I'm an Android developer. Also learning iOS development.

    • Information Management in Shih Hsin University • 相關作品: • Good! 愛點兒 • 世新廣播電台 • 主要語⾔言: • Obj-C / Java / C# / PHP 2 Thursday, August 22, 13
  2. 18 new Thread(new Runnable() { @Override public void run() {

    // ...... } }).start(); Thursday, August 22, 13
  3. new Thread() { @Override public void run() { // ....

    } }.start(); 19 Thursday, August 22, 13
  4. new Thread() { @Override public void run() { // ....

    } }.start(); Thread1 Main Thread(UI Thread) 20 Thursday, August 22, 13
  5. new Thread() { @Override public void run() { // ....

    } }.start(); Thread1 Main Thread(UI Thread) 20 Thursday, August 22, 13
  6. new Thread() { @Override public void run() { // ....

    } }.start(); Thread1 Main Thread(UI Thread) runOnUiThread(new Runnable() { public void run() { // ...... } }); 20 Thursday, August 22, 13
  7. new Thread() { @Override public void run() { // ....

    } }.start(); Thread1 Main Thread(UI Thread) runOnUiThread(new Runnable() { public void run() { // ...... } }); 20 Thursday, August 22, 13
  8. mThread = new HandlerThread("name"); mThread.start(); mThreadHandler = new Handler(mThread.getLooper()) {

    public void handleMessage(android.os.Message msg) { switch (msg.what) { case START_SERVER: // ..... break; case STOP_SERVER: // ..... break; default: break; } } }; 在onCreate()之中 22 Thursday, August 22, 13
  9. HandlerThread1 Main Thread(UI Thread) onCreate() onResume() onPause() onDestroy() mThread =

    new HandlerThread("name"); mThread.start(); 24 Thursday, August 22, 13
  10. HandlerThread1 Main Thread(UI Thread) onCreate() onResume() onPause() onDestroy() mThread =

    new HandlerThread("name"); mThread.start(); if (mThread != null) mThread.quit(); 24 Thursday, August 22, 13
  11. HandlerThread1 Main Thread(UI Thread) onCreate() onResume() onPause() onDestroy() mThread =

    new HandlerThread("name"); mThread.start(); if (mThread != null) mThread.quit(); (Idle) (Running) (Running) (Idle) (Idle) 24 Thursday, August 22, 13
  12. Handler mHandler = new Handler() { @Override public void handleMessage(Message

    msg) { switch (msg.what) { case 0: // ....... break; case 1: // ....... break; case 2: // ....... break; default: break; } } }; 27 Thursday, August 22, 13
  13. obtainMessage(int what); obtainMessage(int what, int arg1, int arg2); obtainMessage(int what,

    int arg1, int arg2, Object obj); obtainMessage(int what, Object obj); ፇᙺᙛ 記得加上.sendToTarget()才會發送 sendEmptyMessage(int what) 32 Thursday, August 22, 13
  14. https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/Activity.java // Reference from Activity.java /** * Runs the specified

    action on the UI thread. If the current thread is the * UI thread, then the action is executed immediately. If the current thread * is not the UI thread, the action is posted to the event queue of the UI * thread. * * @param action * the action to run on the UI thread */ public final void runOnUiThread(Runnable action) { if (Thread.currentThread() != mUiThread) { mHandler.post(action); } else { action.run(); } } 40 Thursday, August 22, 13