最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android中使用AsyncTask做下載進(jìn)度條實例代碼-創(chuàng)新互聯(lián)

android AsyncTask做下載進(jìn)度條

創(chuàng)新互聯(lián)公司長期為1000多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為月湖企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計,月湖網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

AsyncTask是個不錯的東西,可以使用它來做下載進(jìn)度條。代碼講解如下:

package com.example.downloadfile; 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
 
import android.app.Activity; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.widget.TextView; 
 
public class DownloadFile extends Activity { 
   
  public static final String LOG_TAG = "test"; 
   
    private ProgressDialog mProgressDialog; 
  public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
   
   
  File rootDir = Environment.getExternalStorageDirectory(); 
   
  //定義要下載的文件名 
  public String fileName = "test.jpg"; 
  public String fileURL = "/file/tupian/20230213/133227.JPG"; 
   
  @Override 
  public void onCreate(Bundle savedInstanceState)  
  { 
    super.onCreate(savedInstanceState); 
     
    setContentView(R.layout.main); 
    TextView tv = new TextView(this); 
    tv.setText("Android Download File With Progress Bar"); 
   
    //檢查下載目錄是否存在  
    checkAndCreateDirectory("/mydownloads"); 
     
    //執(zhí)行asynctask 
    new DownloadFileAsync().execute(fileURL); 
  } 
  
   
  class DownloadFileAsync extends AsyncTask { 
     
    @Override 
    protected void onPreExecute() { 
      super.onPreExecute(); 
      showDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 
 
     
    @Override 
    protected String doInBackground(String... aurl) { 
 
      try { 
        //連接地址 
        URL u = new URL(fileURL); 
        HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
        c.setRequestMethod("GET"); 
        c.setDoOutput(true); 
        c.connect(); 
         
        //計算文件長度 
        int lenghtOfFile = c.getContentLength(); 
         
         
        FileOutputStream f = new FileOutputStream(new File(rootDir + "/my_downloads/", fileName)); 
      
        InputStream in = c.getInputStream(); 
 
        //下載的代碼 
        byte[] buffer = new byte[1024]; 
        int len1 = 0; 
        long total = 0; 
         
        while ((len1 = in.read(buffer)) > 0) { 
          total += len1; //total = total + len1 
          publishProgress("" + (int)((total*100)/lenghtOfFile)); 
          f.write(buffer, 0, len1); 
        } 
        f.close(); 
         
      } catch (Exception e) { 
        Log.d(LOG_TAG, e.getMessage()); 
      } 
       
      return null; 
    } 
     
    protected void onProgressUpdate(String... progress) { 
       Log.d(LOG_TAG,progress[0]); 
       mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
    } 
 
    @Override 
    protected void onPostExecute(String unused) { 
      //dismiss the dialog after the file was downloaded 
      dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 
  } 
   
   
  public void checkAndCreateDirectory(String dirName){ 
    File new_dir = new File( rootDir + dirName ); 
    if( !new_dir.exists() ){ 
      new_dir.mkdirs(); 
    } 
  } 
   
    @Override 
  protected Dialog onCreateDialog(int id) { 
    switch (id) { 
      case DIALOG_DOWNLOAD_PROGRESS: //we set this to 0 
        mProgressDialog = new ProgressDialog(this); 
        mProgressDialog.setMessage("Downloading file..."); 
        mProgressDialog.setIndeterminate(false); 
        mProgressDialog.setMax(100); 
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
        mProgressDialog.setCancelable(true); 
        mProgressDialog.show(); 
        return mProgressDialog; 
      default: 
        return null; 
    } 
  } 
} 


本文標(biāo)題:Android中使用AsyncTask做下載進(jìn)度條實例代碼-創(chuàng)新互聯(lián)
瀏覽路徑:http://fisionsoft.com.cn/article/cedsii.html