ANDROID使用UI Thread/背景Thread的簡記
Android APP因為有ANR(Application No Respnse)問題要處理所以需要知道哪些操作是要在UI thread上執行, 而複雜操作要在背景thread上執行 寫過Java的人都會想到用Thread或是繼承Thread來執行複雜操作 // Java style thread new Thread(new Runnable() { public void run() { // Running long time process, not included UIs } }).start(); // Override the thread class new Thread() { public void run() { // Running long time process, not included UIs } }.start(); 那Android上如何執行複雜操作? 你還是可以使用Java Thread或是選擇Android提供的Looper與Handler機制 /* import android.os.HandlerThread; import android.os.Handler; HandlerThread mBackLooper = null; Handler mBackHandler = null; mBackLooper = new HandlerThread("BackgroundWorker"); mBackLooper.start(); mBackHandler = new Handler(mBackLooper.getLooper()); */ void processComplexity(String name) { final String Name = name; mBackHandler.post(new Runnable(){ ...