目录
前言
在日常APP的开发中,通常情况下无可避免的要与调用网络后台数据接口。关于Android 网络请求接口的方式可以点击此此处进行学习。当我们实现一个从网络下载文件的功能时候,一般设计思路是这样的:使用Http发起请求,在IntentService的线程进行中下载,再配合Handler更新UI显示保持和用户交互。那么看了本篇就不需要那么麻烦了,因为Google因为帮我们封装好了一个方便下载的API叫做Downloadmanager。下面会先详细介绍实际使用方式,最后将这个API的实现原理。
本案例下载地址:
https://download.youkuaiyun.com/download/csdn_aiyang/10906816
一、介绍
DownloadManager是android2.3(API 9)以后系统提供下载的方法,是处理长期运行的HTTP下载的系统服务。客户端可以请求的URI被下载到一个特定的目标文件。客户端将会在后台与http交互进行下载,或者在下载失败,或者连接改变,重新启动系统后重新下载。还可以进入系统的下载管理界面查看进度。DownloadManger有两个内部类:Request 和Query。Request类可设置下载的一些属性;Query类可查询当前下载的进度等信息。三个公共方法:enqueue、query和remove。enqueue在队列中插入一个新的下载。当连接正常,并且DownloadManager准备执行这个请求时,开始自动下载。返回结果是系统提供的唯一下载ID,这个ID可以用于与这个下载相关的回调。query公共方法,用于查询下载信息。remove公共方法,用于删除下载,如果下载中则取消下载。同时会删除下载文件和记录。
二、使用事项
<uses-permission android:name="android.permission.INTERNET" />;
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>;
创建对象,设置下载地址:
DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
long id = downloadManager.enqueue(request);//每下载的一个文件对应一个id,通过此id可以查询数据
该方法返回成功取消的下载的个数。如果一个下载被取消了,所有相关联的、部分下载的和完全下载的文件都会被删除。
downloadManager.remove(id);
int cancers = downloadManager.remove(id_1, id_2, id_3);
小案例实现代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView down;
private TextView progress;
private TextView file_name;
private ProgressBar pb_update;
private DownloadManager downloadManager;
private DownloadManager.Request request;
public static String downloadUrl = "http://ucdl.25pp.com/fs08/2017/01/20/2/2_87a290b5f041a8b512f0bc51595f839a.apk";
Timer timer;
long id;
TimerTask task;
Handler handler =new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
int pro = bundle.getInt("pro");
String name = bundle.getString("name");
pb_update.setProgress(pro);
progress.setText(String.valueOf(pro)+"%");
file_name.setText(name);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
down = (TextView) findViewById(R.id.down);
progress = (TextView) findViewById(R.id.progress);
file_name = (TextView) findViewById(R.id.file_name);
pb_update = (ProgressBar) findViewById(R.id.pb_update);
down.setOnClickListener(this);
//创建对象
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
request = new DownloadManager.Request(Uri.parse(downloadUrl));
request.setTitle("大象投教");
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
request.setMimeType("application/vnd.android.package-archive");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//创建目录
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdir() ;
//设置文件存放路径
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS ,