91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中使用AsyncTask做下載進度條實例代碼

發布時間:2020-09-20 19:35:04 來源:腳本之家 閱讀:230 作者:lqh 欄目:移動開發

android AsyncTask做下載進度條

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 = "https://cache.yisu.com/upload/information/20200623/125/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<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; 
    } 
  } 
} 

配置文件

注意打開文件保存權限

<?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> 

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

抚州市| 东台市| 长岭县| 玉田县| 松阳县| 平潭县| 德安县| 博野县| 龙胜| 比如县| 长宁县| 阜阳市| 鸡泽县| 双峰县| 宿迁市| 扎囊县| 安远县| 清镇市| 宝坻区| 始兴县| 沁水县| 偃师市| 石林| 汾阳市| 余江县| 玉山县| 洛宁县| 浦北县| 施甸县| 屏山县| 阳春市| 府谷县| 新丰县| 光泽县| 博野县| 东乡族自治县| 重庆市| 南靖县| 青铜峡市| 翁源县| 曲周县|