日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

下载进度条

發(fā)布時(shí)間:2025/7/25 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 下载进度条 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

引用:http://www.pocketdigi.com/20100916/110.html

布局XML:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <ProgressBar android:id="@+id/down_pb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" style="?android:attr/progressBarStyleHorizontal" /> </LinearLayout>

這個(gè)就不用解釋了吧,兩個(gè)控件,一個(gè)是TextView,一個(gè)是橫向條狀進(jìn)度條
程序main.java:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 package com.pocketdigi.download;import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection;import org.apache.http.client.ClientProtocolException; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast;public class main extends Activity {/** Called when the activity is first created. */ProgressBar pb;TextView tv;int fileSize;int downLoadFileSize;String fileEx,fileNa,filename;private Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg){//定義一個(gè)Handler,用于處理下載線程與UI間通訊if (!Thread.currentThread().isInterrupted()){switch (msg.what){case 0:pb.setMax(fileSize);case 1:pb.setProgress(downLoadFileSize);int result = downLoadFileSize * 100 / fileSize;tv.setText(result + "%");break;case 2:Toast.makeText(main.this, "文件下載完成", 1).show();break;case -1:String error = msg.getData().getString("error");Toast.makeText(main.this, error, 1).show();break;}}super.handleMessage(msg);}};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);pb=(ProgressBar)findViewById(R.id.down_pb);tv=(TextView)findViewById(R.id.tv);new Thread(){public void run(){try {down_file("http://wallpaper.pocketdigi.com/upload/1/bigImage/1284565196.jpg","/sdcard/");//下載文件,參數(shù):第一個(gè)URL,第二個(gè)存放路徑} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}.start();}public void down_file(String url,String path) throws IOException{//下載函數(shù) filename=url.substring(url.lastIndexOf("/") + 1);//獲取文件名URL myURL = new URL(url);URLConnection conn = myURL.openConnection();conn.connect();InputStream is = conn.getInputStream();this.fileSize = conn.getContentLength();//根據(jù)響應(yīng)獲取文件大小if (this.fileSize <= 0) throw new RuntimeException("無法獲知文件大小 ");if (is == null) throw new RuntimeException("stream is null");FileOutputStream fos = new FileOutputStream(path+filename);//把數(shù)據(jù)存入路徑+文件名byte buf[] = new byte[1024];downLoadFileSize = 0;sendMsg(0);do{//循環(huán)讀取int numread = is.read(buf);if (numread == -1){break;}fos.write(buf, 0, numread);downLoadFileSize += numread;sendMsg(1);//更新進(jìn)度條} while (true);sendMsg(2);//通知下載完成try{is.close();} catch (Exception ex){Log.e("tag", "error: " + ex.getMessage(), ex);}}private void sendMsg(int flag){Message msg = new Message();msg.what = flag;handler.sendMessage(msg);} }
源代碼:
Android DownloadFile (1966)

轉(zhuǎn)載于:https://www.cnblogs.com/sode/archive/2011/09/26/2191636.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的下载进度条的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。