Android-Handler和Message学习(一)

一、项目需求

1、利用Handler和Message下载网络图片

点击主界面上“下载网络图片”按钮,将会下载一张网络图片。

二、代码实现

1、AndroidManifest.xml文件上添加网络授权

2、activity_main.xml上添加布局




    

3、MainActivity实现代码

注意:在使用HttpClient时候,会提示下面错误,无法导入包,如下图所示。

Android-Handler和Message学习(一)_第1张图片

很简单,在你的gradle里面的android大括号里面加上下面这句,然后重新打开Android Studio,再导入包就可以了。注意,引号是英文的。

useLibrary 'org.apache.http.legacy'

Android-Handler和Message学习(一)_第2张图片

MainActivity.class详细代码

package com.example.zhaoyc.handlerdemo;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private ImageView imageView;
    private String image_path="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";
    private final int IS_FINISH = 1;  //标识符
    private ProgressDialog dialog = null;
    //处理函数
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //把Byte字节数据取出来
            byte[] data = (byte[])msg.obj;
            Bitmap bm = BitmapFactory.decodeByteArray(data,0,data.length);
            imageView.setImageBitmap(bm);
            if (msg.what == IS_FINISH){
                dialog.dismiss();
            }

        }
    };

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

        button = (Button)findViewById(R.id.button);
        imageView = (ImageView)findViewById(R.id.imageView);
        dialog = new ProgressDialog(this);
        dialog.setTitle("提示信息");
        dialog.setMessage("正在下载,请稍候...");
        dialog.setCancelable(false);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new MyThread()).start();
                dialog.show();
            }
        });
    }
    public class MyThread implements Runnable{
        @Override
        public void run() {
            //开辟一个新线程,去处理耗时任务(网络图片下载)
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(image_path);
            HttpResponse httpResponse = null;
            try{
                httpResponse = httpClient.execute(httpGet);//httpClient(http客户端)执行http GET请求
                if (httpResponse.getStatusLine().getStatusCode() ==200){
                    //表示服务器有响应结果
                    byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
                    //将数据发给UI主线程
                    Message message = Message.obtain(); //为什么不去new,注意!
                    message.obj = data;
                    message.what = IS_FINISH;

                    handler.sendMessage(message);
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

}

三、运行效果

Android-Handler和Message学习(一)_第3张图片

你可能感兴趣的:(Android学习,Android,Handler,Message,下载网络图片)