基于Android 下载文件时,更新UI简单帮助类

由于在项目开发时,有这样的简单需求,问谷歌,网络上也有好多Utils工具类,但是比较冗余。自己就简单的写了一个简单帮助类。

/** * 下载文件,更新UI简单帮助类 * * @author jarlen * */{DOWN_BEGIN = 0;DOWN_UPDATA = 1;DOWN_FINISH = 2;DOWN_ERROR = 3;private Context mContext;private TextView mTextView;private ProgressBar mBar;private Handler handler = new Handler(){(Message msg){if (!Thread.currentThread().isInterrupted()){switch (msg.what){case DOWN_BEGIN:if (mTextView != null){mTextView.setText(“开始下载”);}if(mBar != null){mBar.setProgress(0);}break;case DOWN_UPDATA:int factor = msg.arg1;if (mTextView != null){mTextView.setText(factor + “%”);}if(mBar != null){mBar.setProgress(factor);}break;case DOWN_FINISH:if (mTextView != null){mTextView.setText(“下载完成”);}if(mBar != null){mBar.setProgress(100);}break;case DOWN_ERROR:if (mTextView != null){mTextView.setText(“下载错误”);}if(mBar != null){mBar.setProgress(0);}break;default:break;}}};};public DownLoadHelper(Context context){this.mContext = context;}/*** 设置下载时,需要更新的UI TextView* @param view*/(TextView view){this.mTextView = view;}/*** 设置下载时,需要更新的UI,ProgressBar* @param bar*/(ProgressBar bar){this.mBar = bar;}/*** 开始下载* @param url* 文件下载地址* @param path* 文件保存地址*/(final String url, final String path){new Thread(){(){sendMsg(DOWN_BEGIN, 0);try{long downloadSize = downloadUpdateFile(url, path);if (downloadSize > 0){sendMsg(DOWN_FINISH, 0);}} catch (Exception e){e.printStackTrace();sendMsg(DOWN_ERROR, 0);}};}.start();}(String down_url, String path)throws Exception{downloadCount = 0;// 已经下载好的大小int updateCount = 0;// 已经上传的文件大小InputStream inputStream;OutputStream outputStream;URL url = new URL(down_url);HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setConnectTimeout(30 * 1000);httpURLConnection.setReadTimeout(30 * 1000);// 获取下载文件的sizetotalSize = httpURLConnection.getContentLength();if (httpURLConnection.getResponseCode() == 404){sendMsg(DOWN_ERROR, 0);throw new Exception(“fail!”);}inputStream = httpURLConnection.getInputStream();File dir = new File(path);if (!dir.exists()){dir.mkdir();}String name = down_url.substring(down_url.lastIndexOf(“/”) + 1,down_url.length());File updateFile = new File(dir, name);outputStream = [1024];int readsize = 0;while ((readsize = inputStream.read(buffer)) != -1){outputStream.write(buffer, 0, readsize);downloadCount += readsize;(updateCount == 0|| (downloadCount * 100 / totalSize – down_step) >= updateCount){updateCount += down_step;sendMsg(DOWN_UPDATA, updateCount);}}if (httpURLConnection != null){httpURLConnection.disconnect();}inputStream.close();outputStream.close();return downloadCount;}(int flag, int factor){Message msg = new Message();switch (flag){DOWN_ERROR:// 失败break;case DOWN_UPDATA:// 更新进度条msg.arg1 = factor;break;default:break;}msg.what = flag;handler.sendMessage(msg);}}

使用时简单说明下;

DownLoadHelper helper1 = new DownLoadHelper(this);helper1.setUpdataView(tv1);helper1.startDownLoad(“http://img1.2345.com/appsimg/wallpaper/4/139460306960.jpg”, path);

/********************************************/

Demo源码下载地址

没有认真地检查,可能有bug,使用的伙伴请自己debug下,并通知我一下,谢谢

,孤单不是与生俱来,而是由你爱上一个人的那一刻开始。

基于Android 下载文件时,更新UI简单帮助类

相关文章:

你感兴趣的文章:

标签云: