亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android文件下載進(jìn)度條的實(shí)現(xiàn)代碼

 更新時(shí)間:2013年01月21日 11:28:48   作者:  
我們今天開(kāi)始學(xué)習(xí)的是下載進(jìn)度的實(shí)現(xiàn)。今天的這段代碼是網(wǎng)上找的,自己做了些小改,通過(guò)模擬器測(cè)試。文件下載進(jìn)度條控制(就是為了高清壁紙加個(gè)進(jìn)度條),自己研究了好久,但是進(jìn)度條只能顯示緩存寫(xiě)入文件的進(jìn)度,不能顯示下載進(jìn)度。找了好久,終于找到一段用的代碼,所以記錄下來(lái),大家分享
main.xml:
復(fù)制代碼 代碼如下:

<?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" mce_style="?android:attr/progressBarStyleHorizontal"
/>
</LinearLayout>

main.java:
復(fù)制代碼 代碼如下:

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()
   {
     @Override
     public 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);
     }
   };
    @Override
    public 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 block
     e.printStackTrace();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.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("無(wú)法獲知文件大小 ");
     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);
 } 
}

大家看了以后就應(yīng)該明白了,上面寫(xiě)的是用一個(gè)循環(huán)來(lái)完成的這些事情,byte buf[] = new byte[1024];這句話中大家一定要寫(xiě)1024,這個(gè)可不能改變呀。sendMsg(0);這句的括號(hào)里寫(xiě)的是0,這個(gè)也要記得,是0而不是1.

相關(guān)文章

  • Android系統(tǒng)的五種數(shù)據(jù)存儲(chǔ)形式實(shí)例(一)

    Android系統(tǒng)的五種數(shù)據(jù)存儲(chǔ)形式實(shí)例(一)

    Android系統(tǒng)有五種數(shù)據(jù)存儲(chǔ)形式,分別是文件存儲(chǔ)、SP存儲(chǔ)、數(shù)據(jù)庫(kù)存儲(chǔ)、contentprovider 內(nèi)容提供者、網(wǎng)絡(luò)存儲(chǔ)。本篇文章詳解的介紹了每種數(shù)據(jù)存儲(chǔ)的用法,有興趣的可以了解一下。
    2016-12-12
  • android頂部(toolbar)搜索框?qū)崿F(xiàn)代碼

    android頂部(toolbar)搜索框?qū)崿F(xiàn)代碼

    這篇文章主要介紹了android頂部(toolbar)搜索框?qū)崿F(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • AndroidStudio升級(jí)4.1后啟動(dòng)失敗Plugin問(wèn)題解決

    AndroidStudio升級(jí)4.1后啟動(dòng)失敗Plugin問(wèn)題解決

    這篇文章主要介紹了AndroidStudio升級(jí)4.1后啟動(dòng)失敗Plugin問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Android通過(guò)startService播放背景音樂(lè)

    Android通過(guò)startService播放背景音樂(lè)

    這篇文章主要介紹了Android通過(guò)startService播放背景音樂(lè)簡(jiǎn)單示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android自定義view實(shí)現(xiàn)雪花特效實(shí)例代碼

    Android自定義view實(shí)現(xiàn)雪花特效實(shí)例代碼

    實(shí)現(xiàn)雪花的效果其實(shí)也可以通過(guò)自定義View的方式來(lái)實(shí)現(xiàn)的,而且操作上也相對(duì)簡(jiǎn)單一些,下面這篇文章主要給大家介紹了關(guān)于Android自定義view實(shí)現(xiàn)雪花特效的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • Android三種常見(jiàn)的圖片壓縮方式

    Android三種常見(jiàn)的圖片壓縮方式

    在開(kāi)發(fā)中,我們經(jīng)常有這樣一種需求,從相冊(cè)選擇圖片,上傳到服務(wù)器。隨著手機(jī)像素的不斷提升,照片也是小一點(diǎn)的3,4兆,大一點(diǎn)的多大10多兆。如果直接上傳原圖,會(huì)增大服務(wù)器壓力,所以,在上傳之前對(duì)圖片壓縮就顯得很必要了。
    2021-05-05
  • Android圖片或拍照選擇圖片功能實(shí)例代碼

    Android圖片或拍照選擇圖片功能實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于Android圖片或拍照選擇圖片功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • flutter 屏幕尺寸適配和字體大小適配的實(shí)現(xiàn)

    flutter 屏幕尺寸適配和字體大小適配的實(shí)現(xiàn)

    這篇文章主要介紹了flutter 屏幕尺寸適配和字體大小適配的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 最新評(píng)論