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

首頁 > 系統 > Android > 正文

Android實現網絡多線程斷點續傳下載功能

2019-10-23 18:33:27
字體:
來源:轉載
供稿:網友

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

1.多線程下載
2.支持斷點

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

原理

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

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

封裝

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

分配線程

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

線程下載

下面就是各個線程的下載DownThread,上面已經得到了各個線程要下載的初始位置,所以可以通過獲取網絡請求的輸入流InputStream,通過skip()方法跳躍到指定位置進行讀取數據,再寫入到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傳遞參數給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傳遞參數給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傳遞參數給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傳遞一些下載進度信息:

// 靜態注冊廣播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" />

總結

多線程的關鍵就是分配好需要下載的進程,定位進程下載的準確位置,獲取輸入流讀取數據,同時寫入到文件的相應位置??梢越柚鶵andomAccessFile來進行定位。

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 封开县| 锦州市| 雅江县| 江津市| 昌邑市| 岳阳市| 乡宁县| 阿拉尔市| 拜泉县| 弥勒县| 临西县| 托克逊县| 班玛县| 康定县| 通许县| 金昌市| 黄大仙区| 鹤庆县| 福州市| 武陟县| 光泽县| 奈曼旗| 冕宁县| 耒阳市| 云和县| 时尚| 泸州市| 湖州市| 高青县| 永寿县| 通辽市| 延庆县| 青龙| 迭部县| 安宁市| 商水县| 周至县| 大竹县| 嵊泗县| 龙川县| 息烽县|