When downloading files based on Android, update the simple help class of UI and androidui

Source: Internet
Author: User
Tags sendmsg

When downloading files based on Android, update the simple help class of UI and androidui

Due to this simple requirement during project development, Google also has many Utils tool classes on the Internet, but they are relatively redundant. I wrote a simple help class.

/*** Download the file and update the simple UI help class ** @ author jarlen **/public class DownLoadHelper {private static final int DOWN_BEGIN = 0; private static final int DOWN_UPDATA = 1; private static final int DOWN_FINISH = 2; private static final int DOWN_ERROR = 3; private Context mContext; private TextView mTextView; private ProgressBar mBar; private Handler handler = new Handler () {public void handleMessage (Message msg ){ If (! Thread. currentThread (). isInterrupted () {switch (msg. what) {case DOWN_BEGIN: if (mTextView! = Null) {mTextView. setText ("START download");} 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 ("downloaded");} if (mBar! = Null) {mBar. setProgress (100);} break; case DOWN_ERROR: if (mTextView! = Null) {mTextView. setText ("Download error");} if (mBar! = Null) {mBar. setProgress (0) ;}break; default: break ;}};}; public DownLoadHelper (Context context) {this. mContext = context;}/*** sets the UI TextView to be updated * @ param view */public void setUpdataView (TextView view) {this. mTextView = view;}/*** sets the UI to be updated during download. The ProgressBar * @ param bar */public void setUpdataBar (ProgressBar bar) {this. mBar = bar;}/*** start to download * @ param url * file * @ param path * File storage address */public void startDownLoad (final String url, final String path) {new Thread () {public void run () {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 ();} private long downloadUpdateFile (String down_url, String path) throws Exception {int down_step = 1; // The prompt is "step int totalSize"; // The total file size int downloadCount = 0; // The downloaded size int updateCount = 0; // The size of the uploaded file: InputStream inputStream; OutputStream outputStream; URL url = new URL (down_url); HttpURLConnection httpURLConnection = (HttpURLConnection) url. openConnection (); httpURLConnection. setConnectTimeout (30*1000); httpURLConnection. setReadTimeout (30*1000); // get the size of the downloaded object TotalSize = 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 = new FileOutputStream (updateFile, false); // if a file exists, the byte buffer [] = new byte [1024]; int readsize = 0; while (readsize = inputStream. read (buffer ))! =-1) {outputStream. write (buffer, 0, readsize); downloadCount + = readsize; // obtain the downloaded size from time to time // increase by 1% if (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;} private void sendMsg (int flag, int factor) {Message msg = new Message (); switch (flag) {case DOWN_BEGIN: // start case DOWN_FINISH: // complete case DOWN_ERROR: // failed break; case DOWN_UPDATA: // update progress bar msg. arg1 = factor; break; default: break;} msg. what = flag; handler. sendMessage (msg );}}

Simple instructions for use;

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

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

Demo source code
Http://download.csdn.net/detail/jarlen/8552443

If you have not checked it carefully, there may be bugs. If you are using a partner, debug it and notify me. Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.