public class Download implements Serializable{ private static final int START= 1 ; //开始下载 private static final int PUBLISH= 2 ; //更新进度 private static final int PAUSE= 3 ; //暂停下载 private static final int CANCEL= 4 ; //取消下载 private static final int ERROR= 5 ; //下载错误 private static final int SUCCESS= 6 ; //下载成功 private static final int GOON= 7 ; //继续下载 private static ExecutorServicemThreadPool; //线程池 static { mThreadPool=Executors.newFixedThreadPool( 5 ); //默认5个 } private int mDownloadId; //下载id private StringmFileName; //本地保存文件名 private StringmUrl; //下载地址 private StringmLocalPath; //本地存放目录 private boolean isPause= false ; //是否暂停 private boolean isCanceled= false ; //是否手动停止下载 private OnDownloadListenermListener; //监听器 /** *配置下载线程池的大小 *@parammaxSize同时下载的最大线程数 */ public static void configDownloadTheadPool( int maxSize){ mThreadPool=Executors.newFixedThreadPool(maxSize); } /** *添加下载任务 *@paramdownloadId下载任务的id *@paramurl下载地址 *@paramlocalPath本地存放地址 */ public Download( int downloadId,Stringurl,StringlocalPath){ if (! new File(localPath).exists()){ new File(localPath).mkdirs(); } Log.log( "下载地址;" +url); mDownloadId=downloadId; mUrl=url; String[]tempArray=url.split( "/" ); mFileName=tempArray[tempArray.length- 1 ]; mLocalPath=localPath; } /** *设置监听器 *@paramlistener设置下载监听器 *@returnthis */ public DownloadsetOnDownloadListener(OnDownloadListenerlistener){ mListener=listener; return this ; } /** *获取文件名 *@return文件名 */ public StringgetFileName(){ return mFileName; } /** *开始下载 *paramsisGoon是否为继续下载 */ public void start( final boolean isGoon){ //处理消息 final Handlerhandler= new Handler(){ @Override public void handleMessage(Messagemsg){ switch (msg.what){ case ERROR: mListener.onError(mDownloadId); break ; case CANCEL: mListener.onCancel(mDownloadId); break ; case PAUSE: mListener.onPause(mDownloadId); break ; case PUBLISH: mListener.onPublish(mDownloadId,Long.parseLong(msg.obj.toString())); break ; case SUCCESS: mListener.onSuccess(mDownloadId); break ; case START: mListener.onStart(mDownloadId,Long.parseLong(msg.obj.toString())); break ; case GOON: mListener.onGoon(mDownloadId,Long.parseLong(msg.obj.toString())); break ; } } }; //真正开始下载 mThreadPool.execute( new Runnable(){ @Override public void run(){ download(isGoon,handler); } }); } /** *下载方法 *@paramhandler消息处理器 */ private void download( boolean isGoon,Handlerhandler){ Messagemsg= null ; Log.log( "开始下载。。。" ); try { RandomAccessFilelocalFile= new RandomAccessFile( new File( mLocalPath+File.separator+mFileName), "rwd" ); DefaultHttpClientclient= new DefaultHttpClient(); client.setParams(getHttpParams()); HttpGetget= new HttpGet(mUrl); long localFileLength=getLocalFileLength(); final long remoteFileLength=getRemoteFileLength(); long downloadedLength=localFileLength; //远程文件不存在 if (remoteFileLength==-1l){ Log.log( "下载文件不存在..." ); localFile.close(); handler.sendEmptyMessage(ERROR); return ; } //本地文件存在 if (localFileLength>-1l&&localFileLength<remoteFileLength){ Log.log( "本地文件存在..." ); localFile.seek(localFileLength); get.addHeader( "Range" , "bytes=" +localFileLength+ "-" +remoteFileLength); } msg=Message.obtain(); //如果不是继续下载 if (!isGoon){ //发送开始下载的消息并获取文件大小的消息 msg.what=START; msg.obj=remoteFileLength; } else { msg.what=GOON; msg.obj=localFileLength; } handler.sendMessage(msg); HttpResponseresponse=client.execute(get); int httpCode=response.getStatusLine().getStatusCode(); if (httpCode>= 200 &&httpCode<= 300 ){ InputStreamin=response.getEntity().getContent(); byte []bytes= new byte [ 1024 ]; int len=- 1 ; while (- 1 !=(len=in.read(bytes))){ localFile.write(bytes, 0 ,len); downloadedLength+=len; //Log.log((int)(downloadedLength/(float)remoteFileLength*100)); if (( int )(downloadedLength/( float )remoteFileLength* 100 )% 10 == 0 ){ //发送更新进度的消息 msg=Message.obtain(); msg.what=PUBLISH; msg.obj=downloadedLength; handler.sendMessage(msg); //Log.log(mDownloadId+"已下载"+downloadedLength); } //暂停下载,退出方法 if (isPause){ //发送暂停的消息 handler.sendEmptyMessage(PAUSE); Log.log( "下载暂停..." ); break ; } //取消下载,删除文件并退出方法 if (isCanceled){ Log.log( "手动关闭下载。。" ); localFile.close(); client.getConnectionManager().shutdown(); new File(mLocalPath+File.separator+mFileName) .delete(); //发送取消下载的消息 handler.sendEmptyMessage(CANCEL); return ; } } localFile.close(); client.getConnectionManager().shutdown(); //发送下载完毕的消息 if (!isPause)handler.sendEmptyMessage(SUCCESS); } } catch (Exceptione){ //发送下载错误的消息 handler.sendEmptyMessage(ERROR); } } /** *暂停/继续下载 *parampause是否暂停下载 *暂停returntrue *继续returnfalse */ public synchronized boolean pause( boolean pause){ if (!pause){ Log.log( "继续下载" ); isPause= false ; start( true ); //开始下载 } else { Log.log( "暂停下载" ); isPause= true ; } return isPause; } /** *关闭下载,会删除文件 */ public synchronized void cancel(){ isCanceled= true ; if (isPause){ new File(mLocalPath+File.separator+mFileName) .delete(); } } /** *获取本地文件大小 *@return本地文件的大小or不存在返回-1 */ public synchronized long getLocalFileLength(){ long size=-1l; FilelocalFile= new File(mLocalPath+File.separator+mFileName); if (localFile.exists()){ size=localFile.length(); } Log.log( "本地文件大小" +size); return size<= 0 ?-1l:size; } /** *获取远程文件打下or不存在返回-1 *@return */ public synchronized long getRemoteFileLength(){ long size=-1l; try { DefaultHttpClientclient= new DefaultHttpClient(); client.setParams(getHttpParams()); HttpGetget= new HttpGet(mUrl); HttpResponseresponse=client.execute(get); int httpCode=response.getStatusLine().getStatusCode(); if (httpCode>= 200 &&httpCode<= 300 ){ size=response.getEntity().getContentLength(); } client.getConnectionManager().shutdown(); } catch (Exceptione){ e.printStackTrace(); } Log.log( "远程文件大小" +size); return size; } /** *设置http参数不能设置soTimeout *@returnHttpParamshttp参数 */ private static HttpParamsgetHttpParams(){ HttpParamsparams= new BasicHttpParams(); HttpProtocolParams.setContentCharset(params,HTTP.UTF_8); HttpProtocolParams.setUseExpectContinue(params, true ); HttpProtocolParams .setUserAgent( params, "Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/37.0.2041.4Safari/537.36" ); ConnManagerParams.setTimeout(params, 4000 ); HttpConnectionParams.setConnectionTimeout(params, 4000 ); return params; } /** *关闭下载线程池 */ public static void closeDownloadThread(){ if ( null !=mThreadPool){ mThreadPool.shutdownNow(); } } public interface OnDownloadListener{ public void onStart( int downloadId, long fileSize); //回调开始下载 public void onPublish( int downloadId, long size); //回调更新进度 public void onSuccess( int downloadId); //回调下载成功 public void onPause( int downloadId); //回调暂停 public void onError( int downloadId); //回调下载出错 public void onCancel( int downloadId); //回调取消下载 public void onGoon( int downloadId, long localSize); //回调继续下载 } }

如何调用,如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Downloadd= new Download( 1 , "http://baidu.com/test.zip" , "/sdcard/download/" ); d.setOnDownloadListener( new Download.OnDownloadListener(){ @Override public void onSuccess( int downloadId){ System.out.println(downloadId+ "下载成功" ); } @Override public void onStart( int downloadId, long fileSize){ System.out.println(downloadId+ "开始下载,文件大小:" +fileSize); } @Override public void onPublish( int downloadId, long size){ System.out.println( "更新文件" +downloadId+ "大小:" +size); } @Override public void onPause( int downloadId){ System.out.println( "暂停下载" +downloadId); } @Override public void onGoon( int downloadId, long localSize){ System.out.println( "继续下载" +downloadId); } @Override public void onError( int downloadId){ System.out.println( "下载出错" +downloadId); } @Override public void onCancel( int downloadId){ System.out.println( "取消下载" +downloadId); } }); d.start( false );

更多相关文章

  1. 【Android】debug 状态下其签名文件 debug.keystore 相关(如何获
  2. Android 文件上传支持拍照录用录视频
  3. Android 的res/values/colors自定义颜色列表和注释表及布局文件
  4. 【Android】Android消息处理机制
  5. 新建android project和其配置文件的基本介绍
  6. Android使用adb命令直接修改文件
  7. CrossWalk - Android 动态加载so库文件

随机推荐

  1. Android中OnScrollListener的详解(Listvie
  2. android gridview按钮边框和定制点击颜色
  3. android 自定义输入框
  4. Android使用xml自定义图片
  5. android checkbox 定制(修改checkbox 的图
  6. Android我自己的简易(秒表)计时器Chronomet
  7. android技术牛人的博客
  8. Android(安卓)SpannableStringBuilder改
  9. Android(安卓)webkit log定义
  10. android 特效资源