有主线程发送message给子线程

Posted 屌丝迷途

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有主线程发送message给子线程相关的知识,希望对你有一定的参考价值。

 

  通常我们在处理耗时任务时候都会通过新建线程来处理,当任务处理完后通过Handler将结果发送回主线程。比如下面示例:

 1 package com.example.testlistener;
 2 
 3 import java.util.Timer;
 4 import java.util.TimerTask;
 5 
 6 import android.app.Activity;
 7 import android.content.res.Configuration;
 8 import android.os.Bundle;
 9 import android.os.Handler;
10 import android.os.Message;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.view.View.OnLongClickListener;
14 import android.widget.Button;
15 import android.widget.EditText;
16 import android.widget.Toast;
17 
18 public class MainActivity extends Activity {
19     EditText txt;
20     final Handler hander = new Handler(){
21         public void handleMessage(android.os.Message msg) {
22             if(msg.what == 0x1234){
23                 Toast.makeText(MainActivity.this, msg.arg1 + "", 1000).show();
24             }
25         };
26     };
27     @Override
28     protected void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         setContentView(R.layout.activity_main);
31         
32         new Timer().schedule(new TimerTask() {
33             int i = 0;
34             @Override
35             public void run() {
36                 i++;
37                 Message msg = new Message();
38                 msg.what = 0x1234;
39                 msg.arg1 = i;
40                 hander.sendMessage(msg);
41             }
42         }, 0, 2000);
43     }
44     
45 }

 

  那么,我们能不能通过Handler从主线程发送消息给子线程呢?答案是肯定的,需要用到Looper.prepare()和Looper.loop()。如下面的代码:

 1 public class MainActivity extends Activity {
 2     EditText txt;
 3     Button bt;
 4     TestThread testThread;
 5     int j = 0;
 6     
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         txt = (EditText) findViewById(R.id.txt);
12         bt = (Button) findViewById(R.id.bn);
13         testThread = new TestThread();
14         testThread.start();
15         bt.setOnClickListener(new OnClickListener() {
16             
17             @Override
18             public void onClick(View arg0) {
19                 j++;
20                 Message msg = new Message();
21                 msg.what = 0x1000;
22                 msg.arg1 = j;
23                 testThread.thander.sendMessage(msg);
24             }
25         });
26         
27         
28     }
29     
30     //自定义子线程
31     class TestThread extends Thread{
32         public Handler thander;
33         @Override
34         public void run() {
35             //创建一个Looper对象
36             Looper.prepare();
37             thander = new Handler(){
38                 @Override
39                 public void handleMessage(Message msg) {
40                     if(msg.what == 0x1000){
41                         System.out.println("j = " + msg.arg1);
42                         //txt.setText(msg.arg1 + "");
43                     }
44                 }
45             };
46             //启动Looper
47             Looper.loop();
48         }
49     }
50     
51 }

 

以上是关于有主线程发送message给子线程的主要内容,如果未能解决你的问题,请参考以下文章

主线程怎么给子线程发送消息

android handle详解2 主线程给子线程发送消息

android Service中多线程交互

try catch finally中finally代码块有主线程执行?

Handlerthread使用

block的是发送信号的线程,又不是处理槽函数的线程