成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Android中使用AsyncTask做下載進(jìn)度條實例代碼

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

創(chuàng)新互聯(lián)公司是一家網(wǎng)站設(shè)計制作、網(wǎng)站制作,提供網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,網(wǎng)站制作,建網(wǎng)站,按需網(wǎng)站策劃,網(wǎng)站開發(fā)公司,從2013年開始是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價值為核心業(yè)務(wù),全程參與項目的網(wǎng)站策劃設(shè)計制作,前端開發(fā),后臺程序制作以及后期項目運營并提出專業(yè)建議和思路。

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 = "/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"); 
     
    //執(zhí)行asynctask 
    new DownloadFileAsync().execute(fileURL); 
  } 
  
   
  class DownloadFileAsync extends AsyncTask<String, String, String> { 
     
    @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; 
    } 
  } 
} 

配置文件

注意打開文件保存權(quán)限

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
   package="com.example.downloadfile" 
   android:versionCode="1" 
   android:versionName="1.0"> 
   
  <uses-permission android:name="android.permission.INTERNET" /> 
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
   
  <uses-sdk android:minSdkVersion="4" /> 

  <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".DownloadFile" 
         android:label="@string/app_name"> 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 

  </application> 
</manifest> 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

當(dāng)前文章:Android中使用AsyncTask做下載進(jìn)度條實例代碼
網(wǎng)站路徑:http://jinyejixie.com/article14/pgejge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、手機(jī)網(wǎng)站建設(shè)、用戶體驗App設(shè)計、微信公眾號、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)
昭平县| 西盟| 电白县| 肥东县| 景宁| 湖南省| 板桥市| 搜索| 祥云县| 礼泉县| 新巴尔虎右旗| 得荣县| 两当县| 麦盖提县| 正镶白旗| 和顺县| 红桥区| 丁青县| 漯河市| 唐山市| 衡阳县| 禄劝| 平武县| 黄冈市| 贵溪市| 子长县| 莱州市| 麻栗坡县| 溧阳市| 从化市| 清苑县| 虎林市| 文昌市| 松潘县| 沾益县| 库车县| 长子县| 赞皇县| 定结县| 米易县| 广南县|