发布于 2017-09-22 03:04:54 | 97 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了Android实现网络多线程断点续传下载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

我们编写的是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" />


总结

多线程的关键就是分配好需要下载的进程,定位进程下载的准确位置,获取输入流读取数据,同时写入到文件的相应位置。可以借助RandomAccessFile来进行定位。

当然也并非开的线程数越多下载的速度也就越快,因为线程越多对于程序处理这些线程也是一种负担,过多的话反而会降低下载的速度,所以要合理运用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务