Android线程池下载

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/**
*
*/
package com.example.test;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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 com.example.test.Pool.getBitmap;

import android.app.Activity;
import android.content.Entity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

/**
* Description:

* Copyright (c) ,2015 , janson

* This program is protected by copyright laws.

* Program Name:

* Date:
*
* @author
* @version : 1.0
*/
public class Thread_Pool extends Activity implements getBitmap {
private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.pool);
img = (ImageView) findViewById(R.id.imageView1);
new Pool(this, this).poo();
}

@Override
public void getimg(Bitmap bitmap) {
System.out.println("getimg");
img.setImageBitmap(bitmap);
}

}

-----------------------------------------------------------------
/**
*
*/
package com.example.test;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;

/**
* Description:

* Copyright (c) ,2015 , janson

* This program is protected by copyright laws.

* Program Name:

* Date:
*
* @author
* @version : 1.0
*/
public class Pool {
Context c;
getBitmap bitmap2;

public Pool(Context c, getBitmap bitmap2) {
this.c = c;
this.bitmap2 = bitmap2;
}

ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
Bitmap bitmap;
public Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
System.out.println(bitmap);
bitmap2.getimg(bitmap);
};
};

// 可缓存的线程池 线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程
public void poo() {
cachedThreadPool.execute(new Runnable() {

@Override
public void run() {

HttpClient client = new DefaultHttpClient();
HttpGet get = new

HttpGet("https:///img/bdlogo.png");
try {
HttpResponse response = client.execute(get);
byte b[] = EntityUtils.toByteArray(response.getEntity());
bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
handler.sendEmptyMessage(1);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
});
}

public interface getBitmap {
void getimg(Bitmap bitmap);
}
}

相关文档
最新文档