手记

Handler的基本用法

  1. Handler 的Send message ,可以传: what , arg1 ,arg2 ,obj :


  2. package com.example.userrecyclerviewapplication;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    
    
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.ProtocolException;
    import java.net.URL;
    import java.util.List;
    import java.util.Random;
    
    
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    
    public class MainActivity extends AppCompatActivity {
        private Button send, upload;
        private TextView txt;
        private String str = "";
        private Handler handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if(msg.what == 1){
                    txt.setText(str);
                }else if(msg.what == 2){
                    String str2 = "What : "+ msg.what+";arg1:"+msg.arg1+";arg2:"+msg.arg2+";obj:"+((Random)msg.obj).nextInt();
                    txt.setText(str2);
                }
            }
        };
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            initEvent();
    
        }
    
    
        private void initView() {
            send = findViewById(R.id.send_request);
            upload = findViewById(R.id.upload);
            txt = findViewById(R.id.txt);
    
    
        }
    
        private void initEvent() {
            upload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Message message = new Message();
                            message.what = 2;
                            message.arg1 = 111;
                            message.arg2 = 222;
                            message.obj = new Random();
                            handler.sendMessage(message);
                        }
                    }){}.start();
                }
            });
            send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String msg = get();
                            handler.sendEmptyMessage(1);
    
                        }
    
    
                    }) {
                    }.start();
                }
            });
    
    
        }
    
        private String get() {
            try {
                URL url = new URL("http://10.0.2.2/star_male.json");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(6000);
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    InputStream in = connection.getInputStream();
                    byte[] b = new byte[1024];
                    int len = 0;
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    while ((len = in.read(b)) > -1) {
                        baos.write(b, 0, len);
                    }
                  str = new String(baos.toByteArray());
                    return str;
    
    
                }
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "";
    
    
        }
    }
0人推荐
随时随地看视频
慕课网APP