Android中使用AsyncTask實現(xiàn)下載文件動態(tài)更新進度條功能
1. 泛型
AysncTask<Params, Progress, Result>
Params:啟動任務(wù)時傳入的參數(shù),通過調(diào)用asyncTask.execute(param)方法傳入。
Progress:后臺任務(wù)執(zhí)行的進度,若不用顯示進度條,則不需要指定。
Result:后臺任務(wù)結(jié)束時返回的結(jié)果。
2. 重要方法
doInBackground(Params... params):必須重寫的方法,后臺任務(wù)就在這里執(zhí)行,會開啟一個新的線程。params為啟動任務(wù)時傳入的參數(shù),參數(shù)個數(shù)不定。
onPreExecute():在主線程中調(diào)用,在后臺任務(wù)開啟前的操作在這里進行,例如顯示一個進度條對話框。
onPostExecute(Result result):當后臺任務(wù)結(jié)束后,在主線程中調(diào)用,處理doInBackground()方法返回的結(jié)果。
onProgressUpdate(Progress... values):當在doInBackground()中調(diào)用publishProgress(Progress... values)時,返回主線程中調(diào)用,這里的參數(shù)個數(shù)也是不定的。
onCancelled():取消任務(wù)。
3. 注意事項
(1)execute()方法必須在主線程中調(diào)用;
(2)AsyncTask實例必須在主線程中創(chuàng)建;
(3)不要手動調(diào)用doInBackground()、onPreExecute()、onPostExecute()、onProgressUpdate()方法;
(4)注意防止內(nèi)存泄漏,在doInBackground()方法中若出現(xiàn)對Activity的強引用,可能會造成內(nèi)存泄漏。
4. 下載文件動態(tài)更新進度條(未封裝)
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp" tools:context="com.studying.asynctaskdemo.MainActivity"> <ProgressBar android:id="@+id/progressBar" style="?android:progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:progress="0" /> <Button android:id="@+id/download" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="20dp" android:text="@string/start_btn" /> <TextView android:id="@+id/status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="@string/waiting" /> </LinearLayout>
Activity:
public class MainActivity extends Activity { private static final String FILE_NAME = "test.pdf";//下載文件的名稱 private static final String PDF_URL = "http://clfile.imooc.com/class/assist/118/1328281/AsyncTask.pdf"; private ProgressBar mProgressBar; private Button mDownloadBtn; private TextView mStatus; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } private void initView() { mProgressBar = (ProgressBar) findViewById(R.id.progressBar); mDownloadBtn = (Button) findViewById(R.id.download); mStatus = (TextView) findViewById(R.id.status); } private void setListener() { mDownloadBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //AsyncTask實例必須在主線程創(chuàng)建 DownloadAsyncTask asyncTask = new DownloadAsyncTask(); asyncTask.execute(PDF_URL); } }); } /** * 泛型: * String:傳入?yún)?shù)為文件下載地址 * Integer:下載過程中更新ProgressBar的進度 * Boolean:是否下載成功 */ private class DownloadAsyncTask extends AsyncTask<String, Integer, Boolean> { private String mFilePath;//下載文件的保存路徑 @Override protected Boolean doInBackground(String... params) { if (params != null && params.length > 0) { String pdfUrl = params[0]; try { URL url = new URL(pdfUrl); URLConnection urlConnection = url.openConnection(); InputStream in = urlConnection.getInputStream(); int contentLength = urlConnection.getContentLength();//獲取內(nèi)容總長度 mFilePath = Environment.getExternalStorageDirectory() + File.separator + FILE_NAME; //若存在同名文件則刪除 File pdfFile = new File(mFilePath); if (pdfFile.exists()) { boolean result = pdfFile.delete(); if (!result) { return false; } } int downloadSize = 0;//已經(jīng)下載的大小 byte[] bytes = new byte[1024]; int length = 0; OutputStream out = new FileOutputStream(mFilePath); while ((length = in.read(bytes)) != -1) { out.write(bytes, 0, length); downloadSize += length; publishProgress(downloadSize / contentLength * 100); } in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); return false; } } else { return false; } return true; } @Override protected void onPreExecute() { super.onPreExecute(); mDownloadBtn.setText("下載中"); mDownloadBtn.setEnabled(false); mStatus.setText("下載中"); mProgressBar.setProgress(0); } @Override protected void onPostExecute(Boolean aBoolean) { super.onPostExecute(aBoolean); mDownloadBtn.setText("下載完成"); mStatus.setText(aBoolean ? "下載完成" + mFilePath : "下載失敗"); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); if (values != null && values.length > 0) { mProgressBar.setProgress(values[0]); } } } }
5. 下載文件動態(tài)更新進度條(封裝)
Activity:
public class MainActivity extends Activity { private static final String FILE_NAME = "test.pdf"; private static final String PDF_URL = "http://clfile.imooc.com/class/assist/118/1328281/AsyncTask.pdf"; private ProgressBar mProgressBar; private Button mDownloadBtn; private TextView mStatus; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } private void initView() { mProgressBar = (ProgressBar) findViewById(R.id.progressBar); mDownloadBtn = (Button) findViewById(R.id.download); mStatus = (TextView) findViewById(R.id.status); } private void setListener() { mDownloadBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String localPath = Environment.getExternalStorageDirectory() + File.separator + FILE_NAME; DownloadHelper.download(PDF_URL, localPath, new DownloadHelper.OnDownloadListener() { @Override public void onStart() { mDownloadBtn.setText("下載中"); mDownloadBtn.setEnabled(false); mStatus.setText("下載中"); mProgressBar.setProgress(0); } @Override public void onSuccess(File file) { mDownloadBtn.setText("下載完成"); mStatus.setText(String.format("下載完成:%s", file.getPath())); } @Override public void onFail(File file, String failInfo) { mDownloadBtn.setText("開始下載"); mDownloadBtn.setEnabled(true); mStatus.setText(String.format("下載失?。?s", failInfo)); } @Override public void onProgress(int progress) { mProgressBar.setProgress(progress); } }); } }); } }
DownloadHelper:
class DownloadHelper { static void download(String url, String localPath, OnDownloadListener listener) { DownloadAsyncTask task = new DownloadAsyncTask(url, localPath, listener); task.execute(); } private static class DownloadAsyncTask extends AsyncTask<String, Integer, Boolean> { private String mFailInfo; private String mUrl; private String mFilePath; private OnDownloadListener mListener; DownloadAsyncTask(String mUrl, String mFilePath, OnDownloadListener mListener) { this.mUrl = mUrl; this.mFilePath = mFilePath; this.mListener = mListener; } @Override protected Boolean doInBackground(String... params) { String pdfUrl = mUrl; try { URL url = new URL(pdfUrl); URLConnection urlConnection = url.openConnection(); InputStream in = urlConnection.getInputStream(); int contentLength = urlConnection.getContentLength(); File pdfFile = new File(mFilePath); if (pdfFile.exists()) { boolean result = pdfFile.delete(); if (!result) { mFailInfo = "存儲路徑下的同名文件刪除失?。?; return false; } } int downloadSize = 0; byte[] bytes = new byte[1024]; int length; OutputStream out = new FileOutputStream(mFilePath); while ((length = in.read(bytes)) != -1) { out.write(bytes, 0, length); downloadSize += length; publishProgress(downloadSize / contentLength * 100); } in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); mFailInfo = e.getMessage(); return false; } return true; } @Override protected void onPreExecute() { super.onPreExecute(); if (mListener != null) { mListener.onStart(); } } @Override protected void onPostExecute(Boolean aBoolean) { super.onPostExecute(aBoolean); if (mListener != null) { if (aBoolean) { mListener.onSuccess(new File(mFilePath)); } else { mListener.onFail(new File(mFilePath), mFailInfo); } } } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); if (values != null && values.length > 0) { if (mListener != null) { mListener.onProgress(values[0]); } } } } interface OnDownloadListener{ void onStart(); void onSuccess(File file); void onFail(File file, String failInfo); void onProgress(int progress); } }
總結(jié)
以上所述是小編給大家介紹的Android中使用AsyncTask實現(xiàn)下載文件動態(tài)更新進度條功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Jetpack?Compose對比React?Hooks?API相似度
這篇文章主要為大家介紹了Jetpack?Compose對比React?Hooks?API相似度,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08android實現(xiàn)讀取、搜索聯(lián)系人的代碼
本文給大家分享的是在安卓系統(tǒng)中實現(xiàn)讀取搜索聯(lián)系人的代碼,非常的實用,想學(xué)習(xí)安卓開發(fā)的小伙伴一定不要錯過。2015-03-03Android無需讀寫權(quán)限通過臨時授權(quán)讀寫用戶文件詳解
這篇文章主要為大家介紹了Android無需讀寫權(quán)限通過臨時授權(quán)讀寫用戶文件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03Android中使用Toast.cancel()方法優(yōu)化toast內(nèi)容顯示的解決方法
做程序員的,基本一看api就知道,用這個可以取消上一個toast的顯示,然后顯示下一個,這樣就能解決出現(xiàn)的問題??墒窃跍y試的過程中,發(fā)現(xiàn)卻沒有想象中的那么簡單,不信可以百度一下,很多很多人發(fā)現(xiàn)toast的cancel()方法不起作用2013-05-05Android利用WindowManager生成懸浮按鈕及懸浮菜單
這篇文章主要為大家詳細介紹了Android利用WindowManager生成懸浮按鈕及懸浮菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01