欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年01月24日 10:48:12   投稿:lqh  
這篇文章主要介紹了Android中使用AsyncTask做下載進(jìn)度條實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,具有一定參考價值,需要的朋友可以參考下

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

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 = "https://lh4.googleusercontent.com/-HiJOyupc-tQ/TgnDx1_HDzI/AAAAAAAAAWo/DEeOtnRimak/s800/DSC04158.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> 

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

相關(guān)文章

  • Android繪制動態(tài)折線圖

    Android繪制動態(tài)折線圖

    這篇文章主要為大家詳細(xì)介紹了Android繪制動態(tài)折線圖,折線圖隨著手指的滑動進(jìn)行動態(tài)繪制效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Flutter?SystemChrome控制應(yīng)用程序的系統(tǒng)級別行為

    Flutter?SystemChrome控制應(yīng)用程序的系統(tǒng)級別行為

    這篇文章主要為大家介紹了Flutter?SystemChrome用來控制應(yīng)用程序的系統(tǒng)級別行為步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Android 實(shí)現(xiàn)自己的LOG信息

    Android 實(shí)現(xiàn)自己的LOG信息

    本文主要講解Android LOG,這里對如何創(chuàng)建自己的Android LOG信息做了詳細(xì)的介紹,并附簡單代碼示例,有需要的小伙伴可以參考下
    2016-08-08
  • 詳解Flutter手游操縱桿移動的原理與實(shí)現(xiàn)

    詳解Flutter手游操縱桿移動的原理與實(shí)現(xiàn)

    這篇文章將為大家詳細(xì)介紹一下Android?Flutter手游中操縱桿移動角色的原理與實(shí)現(xiàn)過程,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-07-07
  • Android中button點(diǎn)擊后字體的變色效果

    Android中button點(diǎn)擊后字體的變色效果

    button的點(diǎn)擊效果無疑是非常簡單的,接下來通過本文給大家介紹下如何添加button點(diǎn)擊的字體顏色變化效果,感興趣的朋友一起看看吧
    2016-10-10
  • Android利用Intent讀取和更新通訊錄

    Android利用Intent讀取和更新通訊錄

    這篇文章主要介紹了Android利用Intent讀取和更新通訊錄的相關(guān)資料,通過用戶配置文件(user profile)讀取和更新該手機(jī)的所有聯(lián)系人信息,需要的朋友可以參考下
    2016-06-06
  • Android開發(fā)雙向滑動選擇器范圍SeekBar實(shí)現(xiàn)

    Android開發(fā)雙向滑動選擇器范圍SeekBar實(shí)現(xiàn)

    這篇文章主要為大家介紹了Android開發(fā)雙向滑動范圍選擇器SeekBar實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Android調(diào)用系統(tǒng)自帶瀏覽器打開網(wǎng)頁的實(shí)現(xiàn)方法

    Android調(diào)用系統(tǒng)自帶瀏覽器打開網(wǎng)頁的實(shí)現(xiàn)方法

    在Android中可以調(diào)用自帶的瀏覽器,或者指定一個瀏覽器來打開一個鏈接。只需要傳入一個uri,可以是鏈接地址。接下來通過本文給大家分享android 自帶瀏覽器打開網(wǎng)頁的實(shí)現(xiàn)方法,需要的朋友參考下吧
    2017-09-09
  • ComposeDesktop開發(fā)桌面端多功能APK工具

    ComposeDesktop開發(fā)桌面端多功能APK工具

    這篇文章主要為大家介紹了ComposeDesktop開發(fā)桌面端多功能APK工具實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • ffmpeg實(shí)現(xiàn)去水印以及切分視頻demo

    ffmpeg實(shí)現(xiàn)去水印以及切分視頻demo

    這篇文章主要為大家介紹了ffmpeg實(shí)現(xiàn)去水印以及切分視頻demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11

最新評論