Android中使用AsyncTask做下载进度条实例代码-成都快上网建站

Android中使用AsyncTask做下载进度条实例代码

android AsyncTask做下载进度条

创新互联公司是一家网站设计制作、网站制作,提供网页设计,网站设计,网站制作,建网站,按需网站策划,网站开发公司,从2013年开始是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。

AsyncTask是个不错的东西,可以使用它来做下载进度条。代码讲解如下:

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 = "/upload/otherpic74/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"); 
     
    //执行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; 
    } 
  } 
} 

配置文件

注意打开文件保存权限

<?xml version="1.0" encoding="utf-8"?> 
 
   
   
   
   
   

   
     
       
         
         
       
     

   
 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


网站题目:Android中使用AsyncTask做下载进度条实例代码
文章地址:http://kswjz.com/article/pgejge.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流