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
- Android -- 多线程下载, 断点下载
- Android断点下载,多线程下载
- android 多线程下载
- Android 多线程文件下载
- android多线程断点下载
- Android 多线程下载
- Android 多线程下载
- android多线程下载详解
- android多线程下载详解
- android多线程下载详解
- android多线程下载详解
- android多线程下载图片
- android多线程下载
- Android学习 多线程 下载
- android 多线程断点续传下载
- android多线程下载器
- android 多线程断点下载
- android实现多线程下载
- 微信公众帐号开发教程第8篇-文本消息中使用网页超链接
- android ImageButton 和Button的图标区别
- oracle table modified
- 深入理解计算机系统之异常控制流----fork打印问题
- 八大排序算法
- Android 多线程下载
- 简单题
- 使用jquery获取url以及jquery获取url参数的方法
- 翻译www.djangobook.com之第八章:高级视图和URL配置
- Oracle建立全文索引详解
- IPV6编程
- DIV CSS 网页兼容全搞定 (IE6 IE7 IE8 IE9 火狐 谷歌)
- PHP 定位城市
- mysql 中定时执行存储过程