Android--多线程之进程与线程

示例源码:

package com.example.workthreaddemo;

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.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private Button  btnError2,btnRunOnUiThread,btnPost;
    private ImageView imageView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView1=(ImageView)findViewById(R.id.imageView1);
        btnRunOnUiThread = (Button) findViewById(R.id.btnRunOnUiThread);
        btnError2 = (Button) findViewById(R.id.btnError2);
        btnPost = (Button) findViewById(R.id.btnPost);
        
        btnError2.setOnClickListener(new View.OnClickListener() {            
            @Override
            public void onClick(View v) {
                // 增加一个线程访问网络
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // 获取地址下的图片
                        Bitmap btm=loadImageFromNetwork("http://ww4.sinaimg.cn/bmiddle/786013a5jw1e7akotp4bcj20c80i3aao.jpg");
                        imageView1.setImageBitmap(btm);                        
                    }
                }).start();
                
            }
        });
        
        btnRunOnUiThread.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    
                    @Override
                    public void run() {
                        final Bitmap btm=loadImageFromNetwork("http://ww4.sinaimg.cn/bmiddle/786013a5jw1e7akotp4bcj20c80i3aao.jpg");
                        MainActivity.this.runOnUiThread(new Runnable() {                            
                            @Override
                            public void run() {
                                imageView1.setImageBitmap(btm);                                
                            }
                        });
                    }
                }).start();
            }
        });
        
        btnPost.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    
                    @Override
                    public void run() {
                        final Bitmap btm=loadImageFromNetwork("http://ww1.sinaimg.cn/bmiddle/88ff29e8jw1e7pjnpfxbrj20dp0a90tb.jpg");
                        imageView1.post(new Runnable() {
                            
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                imageView1.setImageBitmap(btm);
                            }
                        });
                    }
                }).start();
                
            }
        });
    }
    
    @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;
    }

    
    
    private Bitmap loadImageFromNetwork(String uri)
    {
        Bitmap bitmap=null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(uri);
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                byte[] data = EntityUtils.toByteArray(httpResponse
                        .getEntity());
                bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
                

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

 

posted on 2013-09-12 10:52  猥琐丶欲为  阅读(322)  评论(0编辑  收藏  举报