码迷,mamicode.com
首页 > 编程语言 > 详细

有主线程发送message给子线程

时间:2016-11-14 17:28:47      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:div   example   prot   android   art   view   imp   pre   listener   

 

  通常我们在处理耗时任务时候都会通过新建线程来处理,当任务处理完后通过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给子线程

标签:div   example   prot   android   art   view   imp   pre   listener   

原文地址:http://www.cnblogs.com/l2rf/p/6062104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!