Android多線程下載示例詳解
一、概述
說到Android中的文件下載,Android API中明確要求將耗時的操作放到一個子線程中執(zhí)行,文件的下載無疑是需要耗費(fèi)時間的,所以要將文件的下載放到子線程中執(zhí)行。下面,我們一起來實現(xiàn)一個Android中利用多線程下載文件的小例子。
二、服務(wù)端準(zhǔn)備
在這個小例子中我以下載有道詞典為例,在網(wǎng)上下載有道詞典的安裝包,在eclipse中新建項目web,將下載的有道詞典安裝包放置在WebContent目錄下,并將項目發(fā)布到Tomcat中,具體如下圖所示
三、Android實現(xiàn)
1、布局
界面上自上而下放置一個TextView,用來提示文本框中輸入的信息,一個文本框用來輸入網(wǎng)絡(luò)中下載文件的路徑,一個Button按鈕,點擊下載文件,一個ProgressBar顯示下載進(jìn)度,一個TextView顯示下載的百分比。具體布局內(nèi)容如下:
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下載路徑" /> <EditText android:id="@+id/ed_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="http://192.168.0.170:8080/web/youdao.exe"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下載" android:onClick="download"/> <ProgressBar android:id="@+id/pb" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal"/> <TextView android:id="@+id/tv_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="下載:0%"/> </LinearLayout>
2、自定義ProgressBarListener監(jiān)聽器接口
新建自定義ProgressBarListener監(jiān)聽器接口,這個接口中定義兩個方法,void getMax(int length)用來獲取下載文件的長度,void getDownload(int length);用來獲取每次下載的長度,這個方法中主要是在多線程中調(diào)用,子線程中獲取到的數(shù)據(jù)傳遞到這兩個接口方法中,然后在這兩個接口方法中通過Handler將相應(yīng)的長度信息傳遞到主線程,更新界面顯示信息,具體代碼實現(xiàn)如下:
package com.example.inter; /** * 自定義進(jìn)度條監(jiān)聽器 * @author liuyazhuang * */ public interface ProgressBarListener { /** * 獲取文件的長度 * @param length */ void getMax(int length); /** * 獲取每次下載的長度 * @param length */ void getDownload(int length); }
3、自定義線程類DownloadThread
這里通過繼承Thread的方式來實現(xiàn)自定義線程操作,在這個類中主要是實現(xiàn)文件的下載操作,在這個類中,定義了一系列與下載有關(guān)的實例變量來控制下載的數(shù)據(jù),同時通過自定義監(jiān)聽器ProgressBarListener中的void getDownload(int length)方法來跟新界面顯示的進(jìn)度信息。
具體實現(xiàn)如下:
package com.example.download; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import com.example.inter.ProgressBarListener; /** * 自定義線程類 * @author liuyazhuang * */ public class DownloadThread extends Thread { //下載的線程id private int threadId; //下載的文件路徑 private String path; //保存的文件 private File file; //下載的進(jìn)度條更新的監(jiān)聽器 private ProgressBarListener listener; //每條線程下載的數(shù)據(jù)量 private int block; //下載的開始位置 private int startPosition; //下載的結(jié)束位置 private int endPosition; public DownloadThread(int threadId, String path, File file, ProgressBarListener listener, int block) { this.threadId = threadId; this.path = path; this.file = file; this.listener = listener; this.block = block; this.startPosition = threadId * block; this.endPosition = (threadId + 1) * block - 1; } @Override public void run() { super.run(); try { //創(chuàng)建RandomAccessFile對象 RandomAccessFile accessFile = new RandomAccessFile(file, "rwd"); //跳轉(zhuǎn)到開始位置 accessFile.seek(startPosition); URL url = new URL(path); //打開http鏈接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //設(shè)置超時時間 conn.setConnectTimeout(5000); //指定請求方式為GET方式 conn.setRequestMethod("GET"); //指定下載的位置 conn.setRequestProperty("Range", "bytes="+startPosition + "-" + endPosition); //不用再去判斷狀態(tài)碼是否為200 InputStream in = conn.getInputStream(); byte[] buffer = new byte[1024]; int len = 0; while((len = in.read(buffer)) != -1){ accessFile.write(buffer, 0, len); //更新下載進(jìn)度 listener.getDownload(len); } accessFile.close(); in.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
4、新建DownloadManager類
這個類主要是對下載過程的管理,包括下載設(shè)置下載后文件要保存的位置,計算多線程中每個線程的數(shù)據(jù)下載量等等。
具體實現(xiàn)如下:
package com.example.download; import java.io.File; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import android.os.Environment; import com.example.inter.ProgressBarListener; /** * 文件下載管理器 * @author liuyazhuang * */ public class DownloadManager { //下載線程的數(shù)量 private static final int TREAD_SIZE = 3; private File file; /** * 下載文件的方法 * @param path:下載文件的路徑 * @param listener:自定義的下載文件監(jiān)聽接口 * @throws Exception */ public void download(String path, ProgressBarListener listener) throws Exception{ URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode() == 200){ int filesize = conn.getContentLength(); //設(shè)置進(jìn)度條的最大長度 listener.getMax(filesize); //創(chuàng)建一個和服務(wù)器大小一樣的文件 file = new File(Environment.getExternalStorageDirectory(), this.getFileName(path)); RandomAccessFile accessFile = new RandomAccessFile(file, "rwd"); accessFile.setLength(filesize); //要關(guān)閉RandomAccessFile對象 accessFile.close(); //計算出每條線程下載的數(shù)據(jù)量 int block = filesize % TREAD_SIZE == 0 ? (filesize / TREAD_SIZE) : (filesize / TREAD_SIZE +1 ); //開啟線程下載 for(int i = 0; i < TREAD_SIZE; i++){ new DownloadThread(i, path, file, listener, block).start(); } } } /** * 截取路徑中的文件名稱 * @param path:要截取文件名稱的路徑 * @return:截取到的文件名稱 */ private String getFileName(String path){ return path.substring(path.lastIndexOf("/") + 1); } }
5、完善MainActivity
在這個類中首先,找到頁面中的各個控件,實現(xiàn)Button按鈕的onClick事件,在onClick事件中開啟一個線程進(jìn)行下載操作,同時子線程中獲取到的數(shù)據(jù),通過handler與Message機(jī)制傳遞到主線程,更新界面顯示。
具體實現(xiàn)如下:
package com.example.multi; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.example.download.DownloadManager; import com.example.inter.ProgressBarListener; /** * MainActivity整個應(yīng)用程序的入口 * @author liuyazhuang * */ public class MainActivity extends Activity { protected static final int ERROR_DOWNLOAD = 0; protected static final int SET_PROGRESS_MAX = 1; protected static final int UPDATE_PROGRESS = 2; private EditText ed_path; private ProgressBar pb; private TextView tv_info; private DownloadManager manager; //handler操作 private Handler mHandler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case ERROR_DOWNLOAD: //提示用戶下載失敗 Toast.makeText(MainActivity.this, "下載失敗", Toast.LENGTH_SHORT).show(); break; case SET_PROGRESS_MAX: //得到最大值 int max = (Integer) msg.obj; //設(shè)置進(jìn)度條的最大值 pb.setMax(max); break; case UPDATE_PROGRESS: //獲取當(dāng)前下載的長度 int currentprogress = pb.getProgress(); //獲取新下載的長度 int len = (Integer) msg.obj; //計算當(dāng)前總下載長度 int crrrentTotalProgress = currentprogress + len; pb.setProgress(crrrentTotalProgress); //獲取總大小 int maxProgress = pb.getMax(); //計算百分比 float value = (float)currentprogress / (float)maxProgress; int percent = (int) (value * 100); //顯示下載的百分比 tv_info.setText("下載:"+percent+"%"); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.ed_path = (EditText) super.findViewById(R.id.ed_path); this.pb = (ProgressBar) super.findViewById(R.id.pb); this.tv_info = (TextView) super.findViewById(R.id.tv_info); this.manager = new DownloadManager(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void download(View v){ final String path = ed_path.getText().toString(); //下載 new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { manager.download(path, new ProgressBarListener() { @Override public void getMax(int length) { // TODO Auto-generated method stub Message message = new Message(); message.what = SET_PROGRESS_MAX; message.obj = length; mHandler.sendMessage(message); } @Override public void getDownload(int length) { // TODO Auto-generated method stub Message message = new Message(); message.what = UPDATE_PROGRESS; message.obj = length; mHandler.sendMessage(message); } }); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); Message message = new Message(); message.what = ERROR_DOWNLOAD; mHandler.sendMessage(message); } } }).start(); } }
6、增加權(quán)限
最后,別忘了給應(yīng)用授權(quán),這里要用到Android聯(lián)網(wǎng)授權(quán)和向SD卡中寫入文件的權(quán)限。
具體實現(xiàn)如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.multi" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.multi.MainActivity" 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>
四、運(yùn)行效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android中多線程下載實例
- Android實現(xiàn)多線程下載文件的方法
- android實現(xiàn)多線程下載文件(支持暫停、取消、斷點續(xù)傳)
- Android版多線程下載 仿下載助手(最新)
- Android編程開發(fā)實現(xiàn)帶進(jìn)度條和百分比的多線程下載
- Android實現(xiàn)多線程斷點下載的方法
- Android實現(xiàn)網(wǎng)絡(luò)多線程斷點續(xù)傳下載實例
- Android實現(xiàn)多線程下載圖片的方法
- Android實現(xiàn)網(wǎng)絡(luò)多線程文件下載
- Android編程開發(fā)實現(xiàn)多線程斷點續(xù)傳下載器實例
相關(guān)文章
Android圖片異步加載框架Android-Universal-Image-Loader
這篇文章主要介紹了Android圖片異步加載框架Android-Universal-Image-Loader,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Android中實現(xiàn)監(jiān)聽ScrollView滑動事件
這篇文章主要介紹了Android中實現(xiàn)監(jiān)聽ScrollView滑動事件,本文用重寫ScrollView類的方法實現(xiàn)了一些擴(kuò)展功能,需要的朋友可以參考下2015-05-05android中Intent傳值與Bundle傳值的區(qū)別詳解
本篇文章是對android中Intent傳值與Bundle傳值的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android優(yōu)化之啟動頁去黑屏實現(xiàn)秒啟動
本文的內(nèi)容主要是講Android啟動頁優(yōu)化,去黑屏實現(xiàn)秒啟動的功能,有需要的小伙伴們可以參考學(xué)習(xí)。2016-08-08