Android實現(xiàn)網(wǎng)絡多線程斷點續(xù)傳下載功能
我們編寫的是Andorid的HTTP協(xié)議多線程斷點下載應用程序。直接使用單線程下載HTTP文件對我們來說是一件非常簡單的事。那么,多線程斷點需要什么功能?
1.多線程下載
2.支持斷點
使用多線程的好處:使用多線程下載會提升文件下載的速度
原理
多線程下載的原理就是將要下載的文件分成若干份,其中每份都使用一個單獨的線程進行下載,這樣對于文件的下載速度自然就提高了許多。
既然要分成若干部分分工下載,自然要知道各個線程自己要下載的起始位置,與要下載的大小。所以我們要解決線程的分配與各個線程定位到下載的位置。
封裝
對于多線程下載我們可以將其封裝到一個工具類中DownUtil,向其中傳入下載的鏈接、文件存儲路徑、需要下載的線程數(shù)
分配線程
這里通過HttpURLConnection進行網(wǎng)絡請求下載,通過getContentLength()方法獲取下載文件的總大小,再對其平均分配各個線程需要下載的大小。這樣就確定了下載的大小,下面就是定位到各個線程的開始位置進行下載,這里可以使用RandomAccessFile來追蹤定位到要下載的位置,它的seek()方法可以進行定位。
線程下載
下面就是各個線程的下載DownThread,上面已經(jīng)得到了各個線程要下載的初始位置,所以可以通過獲取網(wǎng)絡請求的輸入流InputStream,通過skip()方法跳躍到指定位置進行讀取數(shù)據(jù),再寫入到RandomAccessFile文件中
一、 編寫基本的UI,三個TextView,分別顯示文件名、下載進度和下載速度,一個ProgressBar。二個Button,分別用于開始下載、暫停下載和取消下載。
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.linux.continuedownload.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="80dp"
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="80dp"
android:id="@+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ProgressBar
android:visibility="invisible"
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開始下載" />
<Button
android:layout_marginLeft="20dp"
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暫停下載" />
<Button
android:layout_marginLeft="20dp"
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消下載" />
</LinearLayout>
</LinearLayout>
在onCreate方法中綁定開始下載按鈕事件:點擊start按鈕,設置進度條可見,并且設置start的Action,啟動服務。
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(fileInfo.getFileName());
progressBar.setVisibility(View.VISIBLE);
// 通過Intent傳遞參數(shù)給service
Intent intent = new Intent(MainActivity.this, DownloadService.class);
intent.setAction(DownloadService.ACTION_START);
intent.putExtra("fileInfo", fileInfo);
startService(intent);
}
});
在onCreate方法中綁定暫停下載按鈕事件:點擊stop按鈕,設置stop的Action,啟動服務。
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 通過Intent傳遞參數(shù)給service
Intent intent = new Intent(MainActivity.this, DownloadService.class);
intent.setAction(DownloadService.ACTION_STOP);
intent.putExtra("fileInfo", fileInfo);
startService(intent);
}
});
在onCreate方法中綁定取消下載按鈕事件:點擊cancel按鈕,設置cancel的Action,啟動服務,之后更新UI。
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 通過Intent傳遞參數(shù)給service
Intent intent = new Intent(MainActivity.this, DownloadService.class);
intent.setAction(DownloadService.ACTION_CANCEL);
intent.putExtra("fileInfo", fileInfo);
startService(intent);
// 更新textView和progressBar的顯示UI
textView.setText("");
progressBar.setVisibility(View.INVISIBLE);
progressView.setText("");
speedView.setText("");
}
});
注冊廣播,用于Service向Activity傳遞一些下載進度信息:
// 靜態(tài)注冊廣播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(DownloadService.ACTION_UPDATE);
registerReceiver(broadcastReceiver, intentFilter);
/**
* 更新UI
*/
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (DownloadService.ACTION_UPDATE.equals(intent.getAction())) {
int finished = intent.getIntExtra("finished", 0);
int speed = intent.getIntExtra("speed", 0);
Log.i("Main", finished + "");
progressBar.setProgress(finished);
progressView.setText(finished + "%");
speedView.setText(speed + "KB/s");
}
}
};
三、 在AndroidManifest.xm文件中聲明權限,定義服務
<service android:name="com.huhx.services.DownloadService" android:exported="true" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
總結
多線程的關鍵就是分配好需要下載的進程,定位進程下載的準確位置,獲取輸入流讀取數(shù)據(jù),同時寫入到文件的相應位置。可以借助RandomAccessFile來進行定位。
當然也并非開的線程數(shù)越多下載的速度也就越快,因為線程越多對于程序處理這些線程也是一種負擔,過多的話反而會降低下載的速度,所以要合理運用。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android多線程斷點續(xù)傳下載實現(xiàn)代碼
- Android 使用AsyncTask實現(xiàn)多線程斷點續(xù)傳
- Android 使用AsyncTask實現(xiàn)多任務多線程斷點續(xù)傳下載
- android實現(xiàn)多線程斷點續(xù)傳功能
- Android多線程斷點續(xù)傳下載示例詳解
- Android FTP 多線程斷點續(xù)傳下載\上傳的實例
- Android多線程+單線程+斷點續(xù)傳+進度條顯示下載功能
- android實現(xiàn)多線程下載文件(支持暫停、取消、斷點續(xù)傳)
- Android實現(xiàn)網(wǎng)絡多線程斷點續(xù)傳下載實例
- Android實現(xiàn)多線程斷點續(xù)傳
相關文章
Android 實現(xiàn)視頻字幕Subtitle和橫豎屏切換示例
下面小編就為大家分享一篇Android 實現(xiàn)視頻字幕Subtitle和橫豎屏切換示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android8.1原生系統(tǒng)網(wǎng)絡感嘆號消除的方法
這篇文章主要介紹了Android8.1原生系統(tǒng)網(wǎng)絡感嘆號消除的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
Android中使用PagerSlidingTabStrip實現(xiàn)導航標題的示例
本篇文章主要介紹了Android中使用PagerSlidingTabStrip實現(xiàn)導航標題的示例,具有一定的參考價值,有興趣的可以了解一下。2017-01-01
Flutter開發(fā)Mac桌面應用實現(xiàn)自動提取生成視頻字幕文件
這篇文章主要為大家介紹了Flutter開發(fā)Mac桌面應用實現(xiàn)自動提取生成視頻字幕文件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
flutter自定義InheritedProvider實現(xiàn)狀態(tài)管理詳解
這篇文章主要為大家介紹了flutter自定義InheritedProvider實現(xiàn)狀態(tài)管理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

