国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實現(xiàn)網(wǎng)絡(luò)多線程斷點續(xù)傳下載功能

2019-12-12 03:15:46
字體:
供稿:網(wǎng)友

我們編寫的是Andorid的HTTP協(xié)議多線程斷點下載應(yīng)用程序。直接使用單線程下載HTTP文件對我們來說是一件非常簡單的事。那么,多線程斷點需要什么功能?

1.多線程下載
2.支持?jǐn)帱c

使用多線程的好處:使用多線程下載會提升文件下載的速度

原理

多線程下載的原理就是將要下載的文件分成若干份,其中每份都使用一個單獨的線程進行下載,這樣對于文件的下載速度自然就提高了許多。

既然要分成若干部分分工下載,自然要知道各個線程自己要下載的起始位置,與要下載的大小。所以我們要解決線程的分配與各個線程定位到下載的位置。

封裝

對于多線程下載我們可以將其封裝到一個工具類中DownUtil,向其中傳入下載的鏈接、文件存儲路徑、需要下載的線程數(shù)

分配線程

這里通過HttpURLConnection進行網(wǎng)絡(luò)請求下載,通過getContentLength()方法獲取下載文件的總大小,再對其平均分配各個線程需要下載的大小。這樣就確定了下載的大小,下面就是定位到各個線程的開始位置進行下載,這里可以使用RandomAccessFile來追蹤定位到要下載的位置,它的seek()方法可以進行定位。

線程下載

下面就是各個線程的下載DownThread,上面已經(jīng)得到了各個線程要下載的初始位置,所以可以通過獲取網(wǎng)絡(luò)請求的輸入流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按鈕,設(shè)置進度條可見,并且設(shè)置start的Action,啟動服務(wù)。

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按鈕,設(shè)置stop的Action,啟動服務(wù)。

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按鈕,設(shè)置cancel的Action,啟動服務(wù),之后更新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文件中聲明權(quán)限,定義服務(wù)

<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" />

總結(jié)

多線程的關(guān)鍵就是分配好需要下載的進程,定位進程下載的準(zhǔn)確位置,獲取輸入流讀取數(shù)據(jù),同時寫入到文件的相應(yīng)位置??梢越柚鶵andomAccessFile來進行定位。

當(dāng)然也并非開的線程數(shù)越多下載的速度也就越快,因為線程越多對于程序處理這些線程也是一種負擔(dān),過多的話反而會降低下載的速度,所以要合理運用。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 朝阳区| 蒙自县| 安徽省| 疏附县| 资兴市| 兴义市| 东城区| 珠海市| 元氏县| 顺义区| 东阿县| 扎赉特旗| 龙南县| 宁安市| 日土县| 井冈山市| 微博| 定日县| 岢岚县| 和龙市| 蕉岭县| 赤城县| 景洪市| 临夏县| 惠来县| 汤阴县| 上高县| 苍梧县| 襄城县| 宣化县| 浠水县| 大新县| 扬中市| 吴旗县| 郧西县| 阆中市| 左贡县| 乐都县| 宣汉县| 平武县| 红河县|