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();
//計(jì)算文件長度
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>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- Android 下載文件通知欄顯示進(jìn)度條功能的實(shí)例代碼
- Android編程實(shí)現(xiàn)顯示在標(biāo)題上的進(jìn)度條功能【附源碼下載】
- Android實(shí)現(xiàn)文件上傳和下載倒計(jì)時功能的圓形進(jìn)度條
- Android中使用AsyncTask實(shí)現(xiàn)下載文件動態(tài)更新進(jìn)度條功能
- android中實(shí)現(xiàn)OkHttp下載文件并帶進(jìn)度條
- android多線程斷點(diǎn)下載-帶進(jìn)度條和百分比進(jìn)度顯示效果
- Android多線程+單線程+斷點(diǎn)續(xù)傳+進(jìn)度條顯示下載功能
- Android帶進(jìn)度條的下載圖片示例(AsyncTask異步任務(wù))
- Android使用AsyncTask下載圖片并顯示進(jìn)度條功能
- Android編程開發(fā)實(shí)現(xiàn)帶進(jìn)度條和百分比的多線程下載
- Android文件下載進(jìn)度條的實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)百分比下載進(jìn)度條效果
相關(guān)文章
Android基于widget組件實(shí)現(xiàn)物體移動/控件拖動功能示例
這篇文章主要介紹了Android基于widget組件實(shí)現(xiàn)物體移動/控件拖動功能,結(jié)合實(shí)例形式分析了widget組件在桌面應(yīng)用中的事件響應(yīng)與屬性動態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
AndroidStudio代碼達(dá)到指定字符長度時自動換行實(shí)例
這篇文章主要介紹了AndroidStudio代碼達(dá)到指定字符長度時自動換行實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android Shape控件美化實(shí)現(xiàn)代碼
本文主要介紹Android Shape 控件的美化, 大家在開發(fā)Android程序的時候?qū)ο到y(tǒng)自帶的控件進(jìn)行修改,這里給大家一個實(shí)例,供大家參考2016-07-07
Android Activity之間的數(shù)據(jù)傳遞方法總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于Android Activity之間的數(shù)據(jù)傳遞方法,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Android可篩選的彈窗控件CustomFiltControl
這篇文章主要為大家詳細(xì)介紹了Android可篩選的彈窗控件CustomFiltControl,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Android應(yīng)用中使用ContentProvider掃描本地圖片并顯示
這篇文章主要介紹了Android應(yīng)用中使用ContentProvider掃描本地圖片并顯示的方法,比調(diào)用本地圖庫的方法更加靈活和可定制,需要的朋友可以參考下2016-04-04

