活用Android的Message Queue(3)

3.  由主线程发送消息给子线程()

    上述范例里,是由子线程发送消息给主线程。本节将介绍如何从主线程发送消息给子线程。其方法是:当子线程执行run()函数时,就创建一个子线程的Handler对象。之后,当主线程执行ac01onClick()函数时,就藉由此Handler对象引用而push消息给子线程。例如下述范例:

 

//----- Looper_04范例 -----

public class ac01 extends Activity implements OnClickListener {

    private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;

    private final int FP = LinearLayout.LayoutParams.FILL_PARENT;

    public TextView tv;

    private myThread t;

    private Button btnbtn2;

    private Handler h;

    private Context ctx;

    public void onCreate(Bundle icicle) {

            super.onCreate(icicle);

            ctx = this;

                LinearLayout layout = new LinearLayout(this);

                layout.setOrientation(LinearLayout.VERTICAL);

                              

                btn = new Button(this);

                btn.setId(101);

                btn.setBackgroundResource(R.drawable.heart);

                btn.setText("test looper");

                btn.setOnClickListener(this);

                LinearLayout.LayoutParams param =

                    new LinearLayout.LayoutParams(100,50);

                param.topMargin = 10;

                layout.addView(btn, param);

               

                btn2 = new Button(this);

                btn2.setId(102);

                btn2.setBackgroundResource(R.drawable.ok_blue);

                btn2.setText("exit");

                btn2.setOnClickListener(this);

                layout.addView(btn2, param);

               

                tv = new TextView(this);

                tv.setTextColor(Color.WHITE);

                tv.setText("");

                LinearLayout.LayoutParams param2 =

                   new LinearLayout.LayoutParams(FPWC);

                param2.topMargin = 10;

                layout.addView(tv, param2);

                setContentView(layout); 

                //------------------------

                t = new myThread();

                 t.start();

          }

          public void onClick(View v) {

        switch(v.getId()){

        case 101:

             String obj = "mainThread";

             Message m = h.obtainMessage(1, 1, 1, obj);

             h.sendMessage(m);

             break;

        case 102:

            h.getLooper().quit();

        finish();

            break;

        }

    }

//------------------------------------------------     

public class EventHandler extends Handler {

          public EventHandler(Looper looper) {

                      super(looper);

          }

           @Override

              public void handleMessage(Message msg) {

                     ((Activity)ctx).setTitle((String)msg.obj);

            }

        }

//------------------------------------------------     

class myThread extends Thread{

     public void run() {

         Looper.prepare();

         h = new Handler(){

               public void handleMessage(Message msg) {

                  EventHandler ha = new

                     EventHandler(Looper.getMainLooper());

                    String obj = (String)msg.obj + ", myThread";

                     Message m = ha.obtainMessage(1, 1, 1, obj);

                     ha.sendMessage(m);

         }

         };

         Looper.loop();

      }

  }

}

 

 

当子线程执行run()函数时,创建一个主线程的EventHandler对象,并且藉之而push消息给主线程了。就进行了两个线程之间的互相交换消息,也是两个函数或对象间之交换消息。此程序输出画面为:

 

 

2

 

     上述范例定义了Thread的子类别。也可以将子线程包含到Runnable类别里,如下:

 

//----- Looper_04aa范例 -----

public class ac01 extends Activity implements OnClickListener {

    private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;

    private final int FP = LinearLayout.LayoutParams.FILL_PARENT;

    public TextView tv;

    private RR r;

    private Button btnbtn2;

    private Handler h;

    private Context ctx;

    public void onCreate(Bundle icicle) {

            super.onCreate(icicle);

            ctx = this;

                LinearLayout layout = new LinearLayout(this);

                layout.setOrientation(LinearLayout.VERTICAL);

                              

                btn = new Button(this);

                btn.setId(101);

                btn.setBackgroundResource(R.drawable.heart);

                btn.setText("test looper");

                btn.setOnClickListener(this);

                LinearLayout.LayoutParams param =

                    new LinearLayout.LayoutParams(100,50);

                param.topMargin = 10;

                layout.addView(btn, param);

               

                btn2 = new Button(this);

                btn2.setId(102);

                btn2.setBackgroundResource(R.drawable.ok_blue);

                btn2.setText("exit");

                btn2.setOnClickListener(this);

                layout.addView(btn2, param);

               

                tv = new TextView(this);

                tv.setTextColor(Color.WHITE);

                tv.setText("");

                LinearLayout.LayoutParams param2 =

                   new LinearLayout.LayoutParams(FPWC);

                param2.topMargin = 10;

                layout.addView(tv, param2);

                setContentView(layout); 

                //------------------------

                r = new RR();

         }

          public void onClick(View v) {

        switch(v.getId()){

        case 101:

             String obj = "mainThread";

             Message m = h.obtainMessage(1, 1, 1, obj);

             h.sendMessage(m);

             break;

        case 102:

            h.getLooper().quit();

        finish();

            break;

        }

    }

//------------------------------------------------      

public class EventHandler extends Handler {

          public EventHandler(Looper looper) {

                      super(looper);

          }

           @Override

              public void handleMessage(Message msg) {

                     ((Activity)ctx).setTitle((String)msg.obj);

            }

        }

//------------------------------------------------     

public class RR implements Runnable {

    public RR() {

        Thread aThread = new Thread(nullthis"RR");

        aThread.start();

    }

    public void run() {

         Looper.prepare();

         h = new Handler(){

               public void handleMessage(Message msg) {

                  EventHandler ha = new EventHandler(Looper.getMainLooper());

                    String obj = (String)msg.obj + ", myThread";

                     Message m = ha.obtainMessage(1, 1, 1, obj);

                     ha.sendMessage(m);

         }

         };

         Looper.loop();

      }

  }

}

 

 

当子线程执行到RR()函数时,创建一个子线程,并执行run()函数,就将消息发送给主线程了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值