Upload
johnny-sung
View
275
Download
4
Embed Size (px)
DESCRIPTION
Citation preview
Handler和Thread的關係
Johnny Sung2013/08/22
多執行緒-
1Thursday, August 22, 13
關於我
• I'm an Android developer. Also learning iOS development.
• Information Management in Shih Hsin University
• 相關作品:
• Good! 愛點兒
• 世新廣播電台
• 主要語⾔言:
• Obj-C / Java / C# / PHP
2Thursday, August 22, 13
Good! 愛點兒
3Thursday, August 22, 13
世新廣播電台
4Thursday, August 22, 13
5Thursday, August 22, 13
6Thursday, August 22, 13
問題
7Thursday, August 22, 13
8Thursday, August 22, 13
9
ANR = Activity Not Responding
http://developer.android.com/training/articles/perf-anr.html
Thursday, August 22, 13
MultiThreading 多執行緒
•Runnable
•Thread
•Handler
•Message
10Thursday, August 22, 13
11Thursday, August 22, 13
Runnable
12Thursday, August 22, 13
13Thursday, August 22, 13
class GetWebPicture implements Runnable{ @Override public void run() { // ........
}}
14Thursday, August 22, 13
Thread顧名思義,它就叫執⾏行緒
15Thursday, August 22, 13
16Thursday, August 22, 13
Thread又有分二種
•單次型 Thread (Java原有的)
•常駐型 HandlerThread (Android特有的)
常駐型 HandlerThread留到後⾯面再說17
Thursday, August 22, 13
18
new Thread(new Runnable(){ @Override public void run() { // ......
}}).start();
Thursday, August 22, 13
new Thread(){ @Override public void run() { // ....
}}.start();
19Thursday, August 22, 13
Main Thread(UI Thread)20Thursday, August 22, 13
Main Thread(UI Thread)20Thursday, August 22, 13
new Thread(){ @Override public void run() { // .... }}.start();
Thread1
Main Thread(UI Thread)20Thursday, August 22, 13
new Thread(){ @Override public void run() { // .... }}.start();
Thread1
Main Thread(UI Thread)20Thursday, August 22, 13
new Thread(){ @Override public void run() { // .... }}.start();
Thread1
Main Thread(UI Thread)
runOnUiThread(new Runnable(){ public void run() { // ......
}});
20Thursday, August 22, 13
new Thread(){ @Override public void run() { // .... }}.start();
Thread1
Main Thread(UI Thread)
runOnUiThread(new Runnable(){ public void run() { // ......
}});
20Thursday, August 22, 13
HandlerThread是⼀一種常駐型的Thread
21Thursday, August 22, 13
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()之中
22Thursday, August 22, 13
在onDestroy()之中
if (mThread != null) mThread.quit();
寫在Service裡⾯面不錯⽤用23
Thursday, August 22, 13
Main Thread(UI Thread)
onCreate()
onResume()
onPause()
onDestroy()
24Thursday, August 22, 13
HandlerThread1
Main Thread(UI Thread)
onCreate()
onResume()
onPause()
onDestroy()
mThread = new HandlerThread("name");mThread.start();
24Thursday, August 22, 13
HandlerThread1
Main Thread(UI Thread)
onCreate()
onResume()
onPause()
onDestroy()
mThread = new HandlerThread("name");mThread.start();
if (mThread != null) mThread.quit();
24Thursday, August 22, 13
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)
24Thursday, August 22, 13
Handler
25Thursday, August 22, 13
26Thursday, August 22, 13
Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) { switch (msg.what) { case 0: // ....... break; case 1: // ....... break; case 2: // ....... break; default: break; } }
};
27Thursday, August 22, 13
Message
28Thursday, August 22, 13
Message就是要一言以蔽之
29Thursday, August 22, 13
用一個值 (一句話)
代表一堆事情(Runnable)
30Thursday, August 22, 13
31Thursday, August 22, 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)
32Thursday, August 22, 13
http://developer.android.com/reference/android/os/Handler.html
其他常用的Method
33Thursday, August 22, 13
我只想寫個小東西
Message & Handler 方式對我來說太複雜了
34Thursday, August 22, 13
有沒有更快的方法?
35Thursday, August 22, 13
有!
36Thursday, August 22, 13
runOnUiThread()
37Thursday, August 22, 13
runOnUiThread(new Runnable(){ public void run() { // ......
}});
38Thursday, August 22, 13
讓我們看一下原始碼
39Thursday, August 22, 13
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(); }}
40Thursday, August 22, 13
41Thursday, August 22, 13
Q & A
42Thursday, August 22, 13
Follow me on Blog
43Thursday, August 22, 13
Follow me on Facebook
44Thursday, August 22, 13