android 多线程下载

本文介绍了如何在Android中实现多线程下载功能。通过设置网络连接权限、创建布局及编辑器,配合下载工具类DownUtil,实现了多线程下载并展示下载进度。在DownUtil类中,每个线程负责下载一部分文件,最后合并成完整文件。
摘要由CSDN通过智能技术生成

首先网络连接设置权限:大笑

 <!-- 在SD卡中创建与删除文件权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <!-- 向SD卡写入数据权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 授权访问网络 -->
    <uses-permission android:name="android.permission.INTERNET" />

布局:xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:text="要下载的资源的URL:"
/>
<EditText
android:id="@+id/url"  
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:text="http://music.baidu.com/data/music/file?link=http://
zhangmenshiting.baidu.com/data2/music/87713529/87600807226800128.mp3
?xcode=24b1557ad2119ecba951d1832bfd71b2ebcd79ff87c48871"
/>
<TextView
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:text="目标文件:"
/>
<EditText
android:id="@+id/target"  
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:text="/mnt/sdcard/a.mp3"
/>
<Button
android:id="@+id/down"  
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:text="@string/down"
/>
<!-- 定义一个水平进度条,用于显示下载进度 -->
<ProgressBar
android:id="@+id/bar"
  android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
/>
</LinearLayout>

下载工具类:

/**
 *
 */
package com.jiaruihuademo.download;


import java.io.InputStream;
import java.io.Rando

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值