找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 2918|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

android Multi-Thread

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:104287 發(fā)表于 2016-1-31 02:39 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
/********************************************************

*2015年3月2日 11:18:37
*功能:通過多線程從互聯(lián)網(wǎng)現(xiàn)在一幅圖片并顯示、
*知識點(diǎn):       
*                        Runnable多線程類
*                        圖片的解碼:BitmapFactory.decodeByteArray(data,0,data.length);
*
*step:1 Runnable多線程類run方法中寫多線程函數(shù)并通過Message類傳給Handler
*                2 Handler中過public void handleMessage(Message msg)獲取多線程中傳回的數(shù)據(jù)
*      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.torrancerestoration.com/bbs/static/image/common/logo.png";
        private final int IS_FINISH=1;
       
        private Handler handler=new Handler(){   //   step3:獲取數(shù)據(jù)

                @Override
                public void handleMessage(Message msg) {
                        // TODO Auto-generated method
                        byte[] data=(byte[])msg.obj;//取書據(jù)
                        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();//獲取網(wǎng)絡(luò)數(shù)據(jù)
                            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());
                                        //把數(shù)據(jù)發(fā)送給Handler
                                            //Message可以攜帶數(shù)據(jù)返回給Handler
                                            Message message=Message.obtain();//攜帶數(shù)據(jù)
                                            message.obj=data;
                                            message.what=IS_FINISH;
                                            handler.sendMessage(message);//發(fā)送到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空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表