高级程序员修炼之道-Android培训系列课程之Thread

更新时间:2023-04-24 06:14:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

学软件,到拓胜浸入式Java培训!全国唯一采用软件工厂模式培训国际化软件人才的IT培训机构,在拓胜软

android

android andriod Handler Looper Handler

Android Looper android UI Handle Handle Handle Looper UI Looper

UI UI UI

Android Looper android UI Handler Handler Handler Looper UI Looper

UI UI UI

Handler Handler

1

2 class ChildThread extends Thread { public void run() { /* * handler Looper. */ Looper.prepare(); /* * handler * */ mChildHandler = new Handler() { public void handleMessage(Message msg) { /* * Do some expensive operations there.

*/

1/8

学软件,到拓胜浸入式Java培训!全国唯一采用软件工厂模式培训国际化软件人才的IT培训机构,在拓胜软

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9 } }; /* * */ Looper.loop(); } }

Handler handleMessage(…)

quit()

mChildHandler.getLooper().quit

();

2/8

学软件,到拓胜浸入式Java培训!全国唯一采用软件工厂模式培训国际化软件人才的IT培训机构,在拓胜软

1

2

3

4

5

6

7

8

9

1

1

1

1

2

1

3

1

4

1

5

1

6

1

7

1

8 /** * * @author allin.dev * * */ public class MainThread extends Activity { private static final String TAG = "MainThread"; private Handler mMainHandler, mChildHandler; private TextView info; private Button msgBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(yout.main); info = (TextView) findViewById(); msgBtn = (Button) findViewById(R.id.msgBtn); mMainHandler = new Handler() { @Override public void handleMessage(Message msg) { Log.i(TAG, "Got an incoming message from the child thread - " + (String) msg.obj); // info.setText((String) msg.obj); } }; new ChildThread().start();

3/8

学软件,到拓胜浸入式Java培训!全国唯一采用软件工厂模式培训国际化软件人才的IT培训机构,在拓胜软

9

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9 msgBtn.setOnClickListener(new OnClickListener() { @Override 2public void onClick(View v) { if (mChildHandler != null) { 2// Message childMsg = mChildHandler.obtainMessage(); 2childMsg.obj = mMainHandler.getLooper().getThread().getName() + " says Hello"; mChildHandler.sendMessage(childMsg); 2Log.i(TAG, "Send a message to the child thread - " + (String)childMsg.obj); } 2} }); 2} public void onDestroy() { 2 super.onDestroy(); Log.i(TAG, "Stop looping the child thread's message queue"); 2mChildHandler.getLooper().quit(); } 2class ChildThread extends Thread { private static final String CHILD_TAG = "ChildThread"; 2public void run() { this.setName("ChildThread"); 3// Handler Looper.prepare(); 3mChildHandler = new Handler() { @Override 3public void handleMessage(Message msg) { Log.i(CHILD_TAG, "Got an incoming message from the main thread - " + 3(String)msg.obj); try { 3// sleep(100); 3Message toMain = mMainHandler.obtainMessage(); toMain.obj = "This is " + this.getLooper().getThread().getName() + 3". Did you send me \"" + (String)msg.obj + "\"?"; mMainHandler.sendMessage(toMain); 3Log.i(CHILD_TAG, "Send a message to the main thread - " + (String)toMain.obj); } catch (InterruptedException e) { 3// TODO Auto-generated catch block e.printStackTrace(); 3} }

4}; 1

4/8

学软件,到拓胜浸入式Java培训!全国唯一采用软件工厂模式培训国际化软件人才的IT培训机构,在拓胜软

1

2

3

4

4

5

4

6

4

7

4

8

4

9

5

5

1

5

2

5

3

5

4

5

5

5

6

5

7

5

8

5

9

6

6

1

5/8 Log.i(CHILD_TAG, "Child handler is bound to - "+ 4mChildHandler.getLooper().getThread().getName()); // 4Looper.loop(); } 4} } 4

学软件,到拓胜浸入式Java培训!全国唯一采用软件工厂模式培训国际化软件人才的IT培训机构,在拓胜软

6

2

6

3

6

4

6

5

6

6

6

7

6

8

6

9

7

7

1

7

2

7

3

7

4

7

5

7

6

7

7

7

8

7

9

8

8

1

8

2

8

6/8

学软件,到拓胜浸入式Java培训!全国唯一采用软件工厂模式培训国际化软件人才的IT培训机构,在拓胜软

3

8

4

8

5

8

6

8

7

8

8

8

9

9

9

1

9

2

9

3

9

4

9

5

9

6

9

7

9

8

9

9

1

00

1

01

1

02

1

03

1

04

7/8

学软件,到拓胜浸入式Java培训!全国唯一采用软件工厂模式培训国际化软件人才的IT培训机构,在拓胜软

1

05

1

06

1

07

1

08

8/8

本文来源:https://www.bwwdw.com/article/u9xq.html

Top