main.xml:
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView android:id="@+id/tv"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text=""<br /> /><br /><ProgressBar android:id="@+id/down_pb"<br />android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:max="100"<br /> style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal"<br />/><br /></LinearLayout>
main.java:
package com.pocketdigi.download;</p><p>import java.io.FileOutputStream;<br />import java.io.IOException;<br />import java.io.InputStream;<br />import java.net.URL;<br />import java.net.URLConnection;</p><p>import org.apache.http.client.ClientProtocolException;<br />import android.app.Activity;<br />import android.os.Bundle;<br />import android.os.Handler;<br />import android.os.Message;<br />import android.util.Log;<br />import android.widget.ProgressBar;<br />import android.widget.TextView;<br />import android.widget.Toast;</p><p>public class main extends Activity {<br /> /** Called when the activity is first created. */<br />ProgressBar pb;<br />TextView tv;<br />int fileSize;<br />int downLoadFileSize;<br />String fileEx,fileNa,filename;<br />private Handler handler = new Handler()<br /> {<br /> @Override<br /> public void handleMessage(Message msg)<br /> {//定義一個Handler,用於處理下載線程與UI間通訊<br /> if (!Thread.currentThread().isInterrupted())<br /> {<br /> switch (msg.what)<br /> {<br /> case 0:<br /> pb.setMax(fileSize);<br /> case 1:<br /> pb.setProgress(downLoadFileSize);<br /> int result = downLoadFileSize * 100 / fileSize;<br /> tv.setText(result + "%");<br /> break;<br /> case 2:<br /> Toast.makeText(main.this, "檔案下載完成", 1).show();<br /> break;</p><p> case -1:<br /> String error = msg.getData().getString("error");<br /> Toast.makeText(main.this, error, 1).show();<br /> break;<br /> }<br /> }<br /> super.handleMessage(msg);<br /> }<br /> };<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> pb=(ProgressBar)findViewById(R.id.down_pb);<br /> tv=(TextView)findViewById(R.id.tv);<br /> new Thread(){<br /> public void run(){<br /> try {<br />down_file("http://wallpaper.pocketdigi.com/upload/1/bigImage/1284565196.jpg","/sdcard/");<br />//下載檔案,參數:第一個URL,第二個存放路徑<br />} catch (ClientProtocolException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />} catch (IOException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}<br /> }<br /> }.start();</p><p> }<br /> public void down_file(String url,String path) throws IOException{<br /> //下載函數<br /> filename=url.substring(url.lastIndexOf("/") + 1);<br /> //擷取檔案名稱<br /> URL myURL = new URL(url);<br /> URLConnection conn = myURL.openConnection();<br /> conn.connect();<br /> InputStream is = conn.getInputStream();<br /> this.fileSize = conn.getContentLength();//根據響應擷取檔案大小<br /> if (this.fileSize <= 0) throw new RuntimeException("無法獲知檔案大小 ");<br /> if (is == null) throw new RuntimeException("stream is null");<br /> FileOutputStream fos = new FileOutputStream(path+filename);<br /> //把資料存入路徑+檔案名稱<br /> byte buf[] = new byte[1024];<br /> downLoadFileSize = 0;<br /> sendMsg(0);<br /> do<br /> {<br /> //迴圈讀取<br /> int numread = is.read(buf);<br /> if (numread == -1)<br /> {<br /> break;<br /> }<br /> fos.write(buf, 0, numread);<br /> downLoadFileSize += numread;</p><p> sendMsg(1);//更新進度條<br /> } while (true);<br /> sendMsg(2);//通知下載完成<br /> try<br /> {<br /> is.close();<br /> } catch (Exception ex)<br /> {<br /> Log.e("tag", "error: " + ex.getMessage(), ex);<br /> }</p><p> }<br />private void sendMsg(int flag)<br />{<br /> Message msg = new Message();<br /> msg.what = flag;<br /> handler.sendMessage(msg);<br />} </p><p>}