Android 实现文件的单线程断点续传下载

网络下载文件是一项很常用的技术,由此断点续传显得尤为重要了,下面来实现一个单线程断点续传的demo,完成项目需要:基础网络知识(Http),了解android界面处理机制,Service的绑定与解绑,BroadCastReceiver的注册与消息的处理,本地文件的I/O处理,数据库基础
摘要由CSDN通过智能技术生成

1.实际效果:

效果图
开始界面


2.代码实现

完成这个小项目需要:

  • 基础网络知识(Http)
  • 了解android界面处理机制
  • Service的绑定与解绑
  • BroadCastReceiver的注册与消息的处理
  • 本地文件的I/O处理
  • 数据库基础
  • 事件回调原理

在这里我采用了数据库框架GreenDao,方便实现想要的效果,自己独立写几个类来操作SQLite数据库也是可以的。关于怎么使用GreenDao,这里不做叙述了,网上一大堆教程,这里给个教程链接http://m.blog.csdn.net/article/details?id=51893092

下面是项目框架
这里写图片描述

主要有服务类DownloadService,广播类ProgressReceiver,下载类DownloadTask,线程信息类ThreadInfo,和MainActivity与App(继承Application,用于初始化数据库)。

首先来看看我的布局文件acitivty_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    tools:context="com.chen.capton.filedownload.MainActivity">

    <TextView
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="文件信息:"
        android:layout_alignEnd="@+id/pause" />

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/info"
        android:layout_alignParentStart="true"
        android:id="@+id/progressBar" />

    <Button
        android:text="开始"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressBar"
        android:layout_alignParentStart="true"
        android:id="@+id/start" />

    <Button
        android:text="暂停"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:id="@+id/pause"
        android:layout_below="@+id/progressBar"
        android:layout_toEndOf="@+id/start" />

    <TextView
        android:text="进度"
        android:gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/progressText"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/info" />
</RelativeLayout>

配置文件menifests.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chen.capton.filedownload">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值