Android更新下载进度条


下载文件会阻塞UI主线程,所以需要new一个新线程来执行下载操作,通过handler执行更新UI进度条操作。代码如下:

  1. public class AndroidTest extends Activity {   
  2.     private static final String TAG = "AndroidTest";   
  3.   
  4.     private ProgressBar progressBar = null;   
  5.     private Button startButton = null;   
  6.     private EditText filenameText = null;   
  7.     private MyHandler handler = null;   
  8.   
  9.     private Message message = null;   
  10.     private boolean flag = true;   
  11.     private int size = 1;   
  12.     private int hasRead = 0;   
  13.     private int len = 0;   
  14.     private byte buffer[] = new byte[1024*4];   
  15.     private int index = 0;    
  16.        
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {   
  19.     super.onCreate(savedInstanceState);   
  20.         setContentView(R.layout.main);   
  21.            
  22.         progressBar = (ProgressBar)findViewById(R.id.progress_horizontal);   
  23.         startButton = (Button)findViewById(R.id.mybutton);   
  24.         startButton.setOnClickListener(new ButtonClick());   
  25.        
  26.         filenameText = (EditText)findViewById(R.id.fileNameID);   
  27.        
  28.         handler = new MyHandler();   
  29.     }   
  30.   
  31.   
  32.     public boolean downloadFile(final String urlStr, final String filename) {   
  33.         new Thread(new Runnable(){     
  34.             public void run() {    
  35.                 try {   
  36.                     URL url = new URL(urlStr);   
  37.                     HttpURLConnection connection = (HttpURLConnection)url.openConnection();   
  38.                     size = connection.getContentLength();   
  39.                     InputStream inputStream = connection.getInputStream();   
  40.                     OutputStream outputStream = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+filename);   
  41.                        
  42.                     while((len=inputStream.read(buffer))!=-1){   
  43.                         outputStream.write(buffer);   
  44.                         hasRead+=len;   
  45.                         index = (int)(hasRead*100)/size;   
  46.                         message = new Message();   
  47.                         message.what = 1;   
  48.                         handler.sendMessage(message);   
  49.                         Log.d(TAG, "index = " + index);   
  50.                         System.out.println("has = "+hasRead+" size = "+size+" index = "+index);   
  51.                     }   
  52.                    
  53.                     inputStream.close();   
  54.                     outputStream.close();   
  55.                 } catch (Exception e) {   
  56.                     flag = false;   
  57.                     e.printStackTrace();   
  58.                 }   
  59.             }   
  60.         }).start();   
  61.            
  62.         return flag;   
  63.     }   
  64.   
  65.     class ButtonClick implements OnClickListener {   
  66.   
  67.         public void onClick(View v) {   
  68.        
  69.             String url = filenameText.getText().toString();   
  70.             String filename = url.substring(url.lastIndexOf('/') + 1);   
  71.             Log.d(TAG, "url = " + url);   
  72.             Log.d(TAG, "filename = " + filename);   
  73.                
  74.             if(!downloadFile(url, filename)) {   
  75.                 String rs = "下载失败 ";   
  76.                 Toast.makeText(AndroidTest.this, rs, Toast.LENGTH_SHORT).show();   
  77.             }   
  78.        
  79.         }   
  80.   
  81.     }   
  82.   
  83.     class MyHandler extends Handler{   
  84.   
  85.         @Override  
  86.         public void handleMessage(Message msg) {   
  87.             if (msg.what == 1) {   
  88.                 progressBar.setProgress(index);   
  89.                 Log.d(TAG, "setProgress index:" + index);   
  90.                 if (index >= 99) {   
  91.                     String rs = "下载完成";   
  92.                     Toast.makeText(AndroidTest.this, rs, Toast.LENGTH_SHORT).show();   
  93.                 }   
  94.             }   
  95.                
  96.             super.handleMessage(msg);   
  97.         }   
  98.   
  99.     }   
  100.   
  101. }  

相关内容