Android 多线程下载

来源:互联网 发布:产品目录软件 编辑:程序博客网 时间:2024/06/07 01:25

效果图:



基于上一节的java多线程下载,使用Handler发送消息至界面,做出响应

主程序:

public class MainActivity extends Activity {private ProgressBar downloadPB;private TextView percentTV;private Handler handler = new Handler() {private int totalLength;public void handleMessage(android.os.Message msg) {switch (msg.what) {case 1:totalLength = msg.getData().getInt("totalLength");downloadPB.setMax(totalLength);break;case 2:int finashLength = msg.getData().getInt("finashLength");downloadPB.setProgress(finashLength);percentTV.setText(finashLength*100/totalLength+"%");break;}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);downloadPB = (ProgressBar) this.findViewById(R.id.downloadPB);percentTV = (TextView) this.findViewById(R.id.percentTV);}public void download(View v) throws Exception {new DownTask("http://dl.360safe.com/360zip_setup_3.2.0.2001.exe",handler).start();}}

下载程序:

public class DownTask extends Thread{private static final String DIR_PATH = "/mnt/sdcard/";private static final int THREAD_ACCOUNT = 3;private URL url;private File dataFile;private File tempFile;private int totalLength;private int threadLength;private int finashLength;private Handler handler;public DownTask(String string, Handler handler) throws Exception {// TODO Auto-generated constructor stuburl = new URL(string);dataFile = new File(DIR_PATH,string.substring(string.lastIndexOf("/")+1));tempFile = new File(dataFile.getAbsolutePath()+".temp");this.handler = handler;}@Overridepublic void run() {// TODO Auto-generated method stubtry {HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(3000);totalLength = conn.getContentLength();threadLength = (totalLength + THREAD_ACCOUNT-1)/THREAD_ACCOUNT;Message msg = new Message();msg.getData().putInt("totalLength", totalLength);msg.what = 1;handler.sendMessage(msg);if (!tempFile.exists()) {RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");for (int i = 0; i < THREAD_ACCOUNT; i++) {raf.writeInt(0);}raf.close();}for (int i = 0; i <THREAD_ACCOUNT; i++) {new DownThread(i).start();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private class DownThread extends Thread{private int id;public DownThread(int id){this.id = id;}@Overridepublic void run() {// TODO Auto-generated method stubtry {RandomAccessFile tempraf = new RandomAccessFile(tempFile, "rws");tempraf.seek(id*4);int threadFinish = tempraf.readInt();synchronized (DownTask.this) {finashLength += threadFinish;}int start = id*threadLength + threadFinish;int end = (id+1)*threadLength - 1;System.out.println("线程" + id + ": " + start + "-" + end);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(3000);conn.setRequestProperty("Range", "bytes="+start+"-"+end);InputStream in = conn.getInputStream();RandomAccessFile dataRaf = new RandomAccessFile(dataFile, "rws");dataRaf.seek(start);byte[] buffer = new byte[1024*100];int len;while((len=in.read(buffer))!=-1){dataRaf.write(buffer,0,len);threadFinish += len;tempraf.seek(id*4);tempraf.writeInt(threadFinish);synchronized (DownTask.this) {finashLength += len;}Message msg = new Message();msg.getData().putInt("finashLength", finashLength);msg.what = 2;handler.sendMessage(msg);}dataRaf.close();tempraf.close();if (finashLength == totalLength) {tempFile.delete();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}


0 0
原创粉丝点击