• 消息队列机制messageQuene


    主线程在创建的同时会创建两个对象消息队列对象(messageQuene)和轮询器对象(Looper)。

    轮询器的作用就是当消息队列有新的消息的时候会通知消息处理器(Handler),这个对象需要我们自己创建,

    消息处理器就会调用hangdleMessage来出来这条消息,因为这个方法运行在主线程,所以可以更新ui。

    总的来说就是一句话,当消息队列里有新消息时候,hangdleMessage这个方法就会被调用,

    子线程如果想更新ui就可以给消息队列发送消息;

    在android4.0之后一些耗时的操作不能放在主线程,比如链接网络,下载等等.

    这时候我们可以放在一个子线程来做这件事,但是子线程无法更新ui界面

    package com.example.getimg;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    
    import android.os.Bundle;
    import android.os.Message;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    
    import java.net.URL;
    
    public class MyActivity extends Activity {
        /**
         * Called when the activity is first created.
         */
        android.os.Handler handler = new android.os.Handler(){
           //用handlemessage来更新ui线程
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case 0:
                        ImageView img = (ImageView) findViewById(R.id.img);
                        img.setImageBitmap((Bitmap) msg.obj);
                        break;
                    case 1:
                        Toast.makeText(MyActivity.this,"没有响应",Toast.LENGTH_SHORT).show();
                        break;
                }
    
            }
        };
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //创建子线程来链接网络,并且使用get方法下载图片
                    Thread thread = new Thread(){
                        @Override
                        public void run() {
                            {
                                String path = "http://192.168.21.1:8080/ok/point_one2.png";
                                //uri
                                try {
                                    URL url = new URL(path);
                                    //获得链接
                                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                                    //设置参数
                                    conn.setRequestMethod("GET");
                                    conn.setConnectTimeout(5000);
                                    conn.setReadTimeout(5000);
                                    //正式链接
                                    conn.connect();
                                    if(conn.getResponseCode()==200){
                                        InputStream is = conn.getInputStream();
                                        //把InputStream通过bitmap转化成图片
                                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                                       //创建消息对象
                                        Message msg = new Message();
                                        msg.what = 0;
                                        //传递bitmap所需要的数据,如果是多个数据可以传递数组啊!
                                        msg.obj = bitmap;
                                        //给消息队列发送消息
                                        handler.sendMessage(msg);
    
                                    }else{
                                        //一般不会new,这样会节省内存
                                        Message msg = handler.obtainMessage();
                                        //传递参数为了判断是谁发送的message
                                        msg.what = 1;
                                        handler.sendMessage(msg);
    //                                    Toast.makeText(MyActivity.this,"没有响应",Toast.LENGTH_SHORT).show();
                                    }
    
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
    
                    };
                    thread.start();
                }
            });
        }
    }

    一般时候message不会直接new出来,而是通过handler.obtainMessage();来进行创建

    msg可以用what传递参数为了判断是谁发送的msg

     
    package com.example.getimg;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;

    import android.os.Bundle;
    import android.os.Message;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.Toast;


    import java.io.InputStream;
    import java.net.HttpURLConnection;

    import java.net.URL;

    public class MyActivity extends Activity {
    /**
    * Called when the activity is first created.
    */
    android.os.Handler handler = new android.os.Handler(){
    //handlemessage来更新ui线程
    @Override
    public void handleMessage(Message msg) {
    switch (msg.what){
    case 0:
    ImageView img = (ImageView) findViewById(R.id.img);
    img.setImageBitmap((Bitmap) msg.obj);
    break;
    case 1:
    Toast.makeText(MyActivity.this,"没有响应",Toast.LENGTH_SHORT).show();
    break;
    }

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

    findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    //创建子线程来链接网络,并且使用get方法下载图片
    Thread thread = new Thread(){
    @Override
    public void run() {
    {
    String path = "http://192.168.21.1:8080/ok/point_one2.png";
    //uri
    try {
    URL url = new URL(path);
    //获得链接
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    //设置参数
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5000);
    conn.setReadTimeout(5000);
    //正式链接
    conn.connect();
    if(conn.getResponseCode()==200){
    InputStream is = conn.getInputStream();
    //InputStream通过bitmap转化成图片
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    //创建消息对象
    Message msg = new Message();
    msg.what = 0;
    //传递bitmap所需要的数据,如果是多个数据可以传递数组啊!
    msg.obj = bitmap;
    //给消息队列发送消息
    handler.sendMessage(msg);

    }else{
    //一般不会new,这样会节省内存
    Message msg = handler.obtainMessage();
    //传递参数为了判断是谁发送的message
    msg.what = 1;
    handler.sendMessage(msg);
    // Toast.makeText(MyActivity.this,"没有响应",Toast.LENGTH_SHORT).show();
    }

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    };
    thread.start();
    }
    });
    }
    }
  • 相关阅读:
    HDU 5791 Two (DP)
    POJ 1088 滑雪 (DPor记忆化搜索)
    LightOJ 1011
    POJ 1787 Charlie's Change (多重背包 带结果组成)
    HDU 5550 Game Rooms (ccpc2015 K)(dp)
    HDU 5542 The Battle of Chibi (ccpc 南阳 C)(DP 树状数组 离散化)
    HDU 5543 Pick The Sticks (01背包)
    HDU 5546 Ancient Go (ccpc2015南阳G)
    NB-IoT的DRX、eDRX、PSM三个模式 (转载,描述的简单易懂)
    MQTT 嵌入式端通讯协议解析(转)
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/4928012.html
Copyright © 2020-2023  润新知