子线程向主线程传递消息

package com.example.day19_handler_demo1;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tv;

    private int count =0;

    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {

            Log.e("AAA", "=handleMessage=>"+Thread.currentThread().getName());
            //处理消息
            //获取消息
            int num = msg.arg1;
            //更新数据
            tv.setText(num+"");

        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.tv);

//       android.view.ViewRootImpl$CalledFromWrongThreadException:
//           Only the original thread that created a view hierarchy
//           can touch its views.


        new Thread(){
            public void run() {
                while(true){

                    if(count<100){
                        count++;
                    }
                    Log.e("AAA", "=Thread=>"+Thread.currentThread().getName());
                //  tv.setText(count+"");
                    //方式一获取Message对象  不建议使用
                    //Message msg = new Message(); 如果用sendToTarget想要setTarget
                    //方式二获取Message对象 
                    //Message msg = Message.obtain(); 方便后面用sendToTarget 
                    //方式三获取Message对象
                    Message msg = handler.obtainMessage();
                    //msg携带数据
                    msg.arg1 = count;
                    //使用Handler发送数据
                    handler.sendMessage(msg);


                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
        }.start();
    }



}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

  • 案例二 Message几个信息载体:
package com.qf.day19_handler_demo2;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {


    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            //接收和处理消息的
            switch (msg.what) {//根据标记进行处理数据
            case 1:
                int a = msg.arg1;
                int a2 = msg.arg2;
                String str = (String) msg.obj;
                int flag = msg.what;

                Log.e("AAA", "==a=="+a);
                Log.e("AAA", "==a2=="+a2);
                Log.e("AAA", "==str=="+str);
                Log.e("AAA", "==flag=="+flag);

                break;
            case 2:
                Log.e("AAAA", "AAAAAAAAAAAAAAAA");

                break;

            default:
                break;
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //子线程里的数据发送到主线程
    public void OnMyClick(View v){

        new Thread(){
            public void run() {

                //获取消息的载体对象
                Message msg = Message.obtain();
                msg.arg1 = 100;
                msg.arg2 = 200;//arg1,arg2:都是int类型数据 
                msg.obj = "我是大数据"; //Object对象   string   Bitmap   List<T>
                msg.what = 1;//int 类型   标记

                handler.sendMessage(msg);

            };
        }.start();

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

  • 案例三 几个发送msg的方式:
package com.qf.day19_handler_demo4;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ImageView ivShow;

    Handler handler = new Handler();

    Handler handler2 = new Handler(){
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 1:
                pDialog.show();
                break;
            case 2:

                Bitmap bitmap = (Bitmap) msg.obj;
                ivShow.setImageBitmap(bitmap);

                pDialog.dismiss();

                break;

            default:
                break;
            }


        };
    };


    private ProgressDialog pDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ivShow = (ImageView) findViewById(R.id.iv_show);

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setTitle("下载图片");
        pDialog.setIcon(R.drawable.ic_launcher);
        pDialog.setMessage("正在下载中,请稍后...");
    }

    public void MyLoadClick(View v) {
        //pDialog.show();
        //开启子线程下载图片
        new Thread(){
            public void run() {
                //在子线程展示

                //发送null消息  只设置一个标记
                handler2.sendEmptyMessage(1);


                byte[] data = HttpUtil.getHttpResult("https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2507878052,3446525205&fm=58");
                final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                //第一种方式   将代码块发送到主线程 在内部直接一个匿名内部类runnable 可以直接更新UI(主线程)
//              handler.post(new Runnable() {
//                  
//                  @Override
//                  public void run() {
//                      // TODO Auto-generated method stub
//                      Log.e("AAA", "=handler=>"+Thread.currentThread().getName());
//                  
//                      //更新Ui
//                      ivShow.setImageBitmap(bitmap);
//                      pDialog.dismiss();
//                  }
//              });

                //延迟发送 在内部直接一个匿名内部类runnable 可以直接更新UI(主线程)
//              handler.postDelayed(r, delayMillis)


                //第二种方式     使用msg  携带数据到主线程
//              Message msg = Message.obtain();
//              msg.obj = bitmap;
//              msg.what = 2;
//              handler2.sendMessage(msg);

//              延迟发送一个msg
                //handler2.sendMessageDelayed(msg, delayMillis)

                //第三种   sendToTarget  发送到那个Handler
//              Message msg = handler2.obtainMessage();
//              msg.obj = bitmap;
//              msg.what = 2;
//              msg.sendToTarget();//发送给那个Handler

                //第四种方式
                Message msg = Message.obtain();
                msg.obj = bitmap;
                msg.what = 2;
                msg.setTarget(handler2);//指明是那个Handler
                msg.sendToTarget();//发送给那个Handler



            };
        }.start();

    }

}
阅读更多

没有更多推荐了,返回首页