找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2408|回复: 0
打印 上一主题 下一主题
收起左侧

android Multi-Thread

[复制链接]
跳转到指定楼层
楼主
ID:104287 发表于 2016-1-31 02:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/********************************************************

*2015年3月2日 11:18:37
*功能:通过多线程从互联网现在一幅图片并显示、
*知识点:       
*                        Runnable多线程类
*                        图片的解码:BitmapFactory.decodeByteArray(data,0,data.length);
*
*step:1 Runnable多线程类run方法中写多线程函数并通过Message类传给Handler
*                2 Handler中过public void handleMessage(Message msg)获取多线程中传回的数据
*      3 onClick()通过new Thread(new MyThread()).start();开启线程
*/

package com.example.handler;

import java.io.InputStream;

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;

import android.R.string;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

       
        private Button button;
        private ImageView imagineView;
        private String image_path="http://www.51hei.com/bbs/static/image/common/logo.png";
        private final int IS_FINISH=1;
       
        private Handler handler=new Handler(){   //   step3:获取数据

                @Override
                public void handleMessage(Message msg) {
                        // TODO Auto-generated method
                        byte[] data=(byte[])msg.obj;//取书据
                        Bitmap bm=BitmapFactory.decodeByteArray(data,0,data.length);//图片解码
                        imagineView.setImageBitmap(bm);       
                        if(msg.what==IS_FINISH)
                        {
                                Toast.makeText(MainActivity.this,"下载成功",Toast.LENGTH_SHORT).show();
                        }
                       
                        super.handleMessage(msg);
                }
       
               
        };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)this.findViewById(R.id.button1);
        imagineView=(ImageView)this.findViewById(R.id.imageView1);
        button.setOnClickListener(new View.OnClickListener() {
                       
                        @Override
                        public void onClick(View arg0) {

                                // TODO Auto-generated method stub
                new Thread(new MyThread()).start();      //step4:开线程
                        }
                });
    }


    public class MyThread implements Runnable      //step1:定义多线程类
    {
             @Override
                    public void run() { //   step2:线程耗时,完成下载
                            // TODO Auto-generated method stub

                            HttpClient httpClient=new DefaultHttpClient();//获取网络数据
                            HttpGet httpGet=new HttpGet(image_path);
                            HttpResponse httpResponse=null;
                            //InputStream inputStream=null;
                           
                            try{
                                    httpResponse=httpClient.execute(httpGet);
                                    if(httpResponse.getStatusLine().getStatusCode()==200)
                                    {
                                            //inputStream=httpResponse.getEntity().getContent();
                                            byte[]data=EntityUtils.toByteArray(httpResponse.getEntity());
                                        //把数据发送给Handler
                                            //Message可以携带数据返回给Handler
                                            Message message=Message.obtain();//携带数据
                                            message.obj=data;
                                            message.what=IS_FINISH;
                                            handler.sendMessage(message);//发送到handler
                                    }
                            }catch(Exception e)
                            {
                                    Toast.makeText(MainActivity.this,"捕获异常信息1",Toast.LENGTH_SHORT).show();
                            }
                           
                     
                    }
        }



        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表