Android更新下载进度条

本文介绍了一种在Android应用中实现文件下载的方法,并通过创建新线程避免阻塞UI主线程,同时利用Handler更新进度条显示下载进度。

下载文件会阻塞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.  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值